I am struggling with this piece of Python code. The problem is,when a user enters something wrong I need my code to keep looping until they input a valid answer. This is how the
Update:
Here is the first part of your code with a while (condition):
Order = [ 'No drink', 'No food' ]
Yesses = [ 'y', 'yes', 'yep', 'okay', 'sure' ]
DRINK = 0
FOOD = 1
print("Welcome to Hungary house!")
print("")
print("First, let me get your drink order, if any.")
answer = input("Would you like something to drink? ").lower()
if answer in Yesses:
print("What drink would you prefer?")
print ("1: Fanta")
print ("2: Coke")
print ("3: Pepsi")
print ("4: Sprite")
correct = False
while (!correct):
choice = input("Please enter a drink from the menu above\n").lower()
if (choice == "1" or choice == "fanta"):
Order[DRINK] = "Fanta"
correct = True
elif (choice == "2" or choice == "coke"):
Order[DRINK] = "Coke"
correct = True
elif (choice == "3" or choice == "pepsi"):
Order[DRINK] = "Pepsi"
correct = True
elif (choice == "4" or choice == "sprite"):
Order[DRINK] = "Sprite"
correct = True
print ("You have chosen: " + Order[DRINK])
Original answer:
So if it's using input()
you can get away with doing a while loop until the statement is true. So put all the code in a loop and fulfill the Boolean when correct. For example:
correct = false
while (!correct):
var = input ("your statement")
if var == "good":
correct = true
#next stuff