I am trying to write a program that appends each line of user input to a list. Once the user enters ”, the loop should break. When their is more than 1 line of input the code executes the way it is intended.
However, there is an elif statement for when a users first input is ”, that asks the user to enter something besides ”.
Upon testing this part out, the program returns ‘Enter a list’ line you can see below, but then it ignores the rest of the program. If you enter ” again, the program exits and outputs [] when it should be going through the loop until there is a user input that is not ”
I’ve tried to do this with and without a counter variable and I’m not getting anywhere.
list1 = list()
def listt():
counter = 0
print("Please enter your shopping list:")
while counter < 1:
ask = input()
list1.append(ask)
if len(list1) > 1 and list1[-1] == '':
list1.remove(list1[-1])
counter += 1
break
elif len(list1) == 1 and list1[0] == '':
print('Enter a shopping list:')
print(list1)
listt()
Charles Robinson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.