I'm writing a program in Python that asks a user what toppings they would like on their pizza, as shown below.
print("PIZZA TOPPINGS: \n mushrooms \n pineapple \n anchovies \n pepperoni \n sausage \n extra cheese \n")
toppings = ["pepperoni", "ham", "pineapple", "mushrooms", "sausage", "anchovies", "extra cheese"]
your_toppings = []
maxLengthList = 3
while len(your_toppings) < maxLengthList:
item = input("Add up to 3 toppings: ").lower()
if item == "ham":
print("Sorry, out of ham")
continue
elif item in toppings:
print("Adding " + item)
elif item == "":
ans = input("Are you sure you don't want to add any more toppings? Y/N ").lower() #this is the line I'm trying to loop back to
if ans == "yes" or ans == "y":
break
elif ans == "no" or ans == "n":
continue
else:
print("Please answer y/n")
#trying to loop back to Y/N prompt
else:
print("Not an option")
continue
your_toppings.append(item)
if len(your_toppings) > 0:
print("Your toppings: " + ', '.join(your_toppings))
else:
print("No toppings for you!")
The part I've been stuck at is inside the nested If statement (see the 2nd comment), if the user does not answer with the required y/n. I'm trying to figure out how I can loop back to the input prompt, "Are you sure you don't want to add any more toppings? Y/N" until they respond with y/n.
I did try to use a nested while loop before the ans = input line, but that breaks the program as answering with y/n does not break/continue in the original while loop (i.e. back to the topping prompt), but in the inner while loop instead.
Is there a simpler way of accomplishing this?
I'm still pretty new to Python so I'm playing around primarily with these If statements/While loops at this point, so I understand my program may start to look messy with so much nesting. I am willing to modify it using another angle if that would accomplish the same thing.
Thanks to everyone in advance!
Aucun commentaire:
Enregistrer un commentaire