Reputation: 55
I'm working on an exercise that involves making a small scaled grocery app using python. Here is the description:
Write a program to keep track of current grocery list and current pantry items. It should ask for the user to ask for which items that they have bought from the grocery list. If they have bought the item, remove the item from grocery list and add the item to list of pantry items.
I have attempted to develop this but the user input is not affecting the list.
print("GROCERY LIST APP")
grocerylist = ["Apple", "Onion", "Flour"]
print("You have items in your grocery list:", grocerylist)
updatedgrocerylist = []
itemsinpantry = []
userinput = input("Have you bought Apple?")
userinput2 = input("Have you bought Onion?")
userinput3 = input("Have you brought Flour?")
if userinput == "Yes" or userinput == "yes":
itemsinpantry.append('Apple')
elif userinput == "No" or userinput == "no":
updatedgrocerylist.append('Apple')
elif userinput2 == "Yes" or userinput2 == "yes":
itemsinpantry.append('Onion')
elif userinput2 == "No" or userinput2 == "no":
updatedgrocerylist.append('Onion')
elif userinput3 == "Yes" or userinput3 == "yes":
itemsinpantry.append('Flour')
elif userinput3 == "No" or userinput3 == "no":
updatedgrocerylist.append('Flour')
print("Your updated grocery list:", updatedgrocerylist)
print("Items in the pantry:", itemsinpantry)
I expect the program to append the fruits to the list based on user decision.
Upvotes: 0
Views: 131
Reputation: 71
The reason the output from Yes, No, Yes is as so, is because of your nested if structuring.
Once the program accepts the first variable "userinput" it will skip the other elif
statements and go to the two print
functions at the bottom.
I have provided a possible solution:
print("GROCERY LIST APP")
grocerylist = ["Apple", "Onion", "Flour"]
print("You have items in your grocery list:", grocerylist)
updatedgrocerylist = []
itemsinpantry = []
userinput = input("Have you bought Apple?")
if userinput == "Yes" or userinput == "yes":
itemsinpantry.append('Apple')
elif userinput == "No" or userinput == "no":
updatedgrocerylist.append('Apple')
userinput2 = input("Have you bought Onion?")
if userinput2 == "Yes" or userinput2 == "yes":
itemsinpantry.append('Onion')
elif userinput2 == "No" or userinput2 == "no":
updatedgrocerylist.append('Onion')
userinput3 = input("Have you brought Flour?")
if userinput3 == "Yes" or userinput3 == "yes":
itemsinpantry.append('Flour')
elif userinput3 == "No" or userinput3 == "no":
updatedgrocerylist.append('Flour')
Hope this helps!
Upvotes: 3
Reputation: 31
find corrected code
print("GROCERY LIST APP")
grocerylist = ["Apple", "Onion", "Flour"]
print("You have items in your grocery list:", grocerylist)
updatedgrocerylist = []
itemsinpantry = []
userinput = input("Have you bought Apple?")
userinput2 = input("Have you bought Onion?")
userinput3 = input("Have you brought Flour?")
if userinput == "Yes" or userinput == "yes":
itemsinpantry.append('Apple')
else:
updatedgrocerylist.append('Apple')
if userinput2 == "Yes" or userinput2 == "yes":
itemsinpantry.append('Onion')
else:
updatedgrocerylist.append('Onion')
if userinput3 == "Yes" or userinput3 == "yes":
itemsinpantry.append('Flour')
else:
updatedgrocerylist.append('Flour')
print("Your updated grocery list:", updatedgrocerylist)
print("Items in the pantry:", itemsinpantry)
Upvotes: 0