Reputation: 3
I wrote a program that receives from the user one string representing a shopping list. The program asks the user to enter a number between 1 and 9. Depending on the number received, do one of the following: And after making a user selection, the user returns to the main menu until they select number 9 to exit. The syntax is correct but the program does not print what is needed. How to fix it?
def shopping_list(my_str):
my_list = my_str.split(",")
i = input("Please choose a number between 1 and 9: ")
while i in range(1, 10):
if i == 1:
print("My shopping list:", my_list)
continue
elif i == 2:
print("The number of items in my shopping list:", len(my_list))
continue
elif i == 3:
product = input("Please enter a product name: ")
if product in my_list:
print("This product is in the shopping list.")
else:
print("This item is not in the shopping list.")
continue
elif i == 4:
product = input("Please enter a product name: ")
print("The item", product, "shows", my_list.count(product), "in the list")
continue
elif i == 5:
product = input("Please enter a product name: ")
new_list = my_list.remove(product)
print("The item", product, "remove from the list. The new list is", new_list)
continue
elif i == 6:
product = input("Please enter a product name: ")
my_list += product
print("The item", product, " add to the list. The new list is", my_list)
continue
elif i == 7:
new_list = []
for product in my_list:
if len(product) < 3 or not(product.isalpha()):
new_list += product
continue
elif i == 8:
print(list(set(my_list)))
continue
else:
break
shopping_list("Milk,Cottage,Tomatoes")
Upvotes: 0
Views: 177
Reputation:
I would do this: (Cut out some elifs due to too much code.)
while True:
i = int(input("Please choose a number between 1 and 9: "))
if i == 1:
print("My shopping list:", my_list)
continue
elif i == 8:
print(list(set(my_list)))
continue
elif i == 9:
break
else:
print("Invalid Number! Try again.")
Is this what you want? I'm not quite getting what you're asking for.
Upvotes: 0
Reputation: 54168
continue
statement, you don't need them as all code is in elif
also it'll allow you to ask the user a new choice at the end of the loop.int
, you won't be able to enter the loop withoutdef shopping_list(my_str):
my_list = my_str.split(",")
i = int(input("Please choose a number between 1 and 9: "))
while i in range(1, 10):
if i == 1:
print("My shopping list:", my_list)
elif i == 2:
print("The number of items in my shopping list:", len(my_list))
elif i == 3:
# ...
elif i == 8:
print(list(set(my_list)))
else:
break
i = int(input("Please choose a number between 1 and 9: "))
Now corrections about
mode 5
: what is returned by remove
is None
, the modification is in-placed so do
elif i == 5:
product = input("Please enter a product name: ")
my_list.remove(product)
print("The item", product, "remove from the list. The new list is", my_list)
mode 6
the operator +=
does an extend
on the list so it'll add all chars, do append
instead
elif i == 6:
product = input("Please enter a product name: ")
my_list.append(product)
print("The item", product, " add to the list. The new list is", my_list)
mode 7
creating a new list that is a filter of the main one is useless if you forget it. Also I'd say you remove the items that are smaller than 3 or contains non-alpha, here you keep them. Finally use append
elif i == 7:
new_list = []
for product in my_list:
if len(product) >= 3 and product.isalpha():
new_list.append(product)
my_list = list(new_list)
or just use a list comprehension
elif i == 7:
my_list = [p for p in my_list if len(p) >= 3 and p.isalpha()]
Upvotes: 1