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
while True:
choice = input("Please enter a drink from the menu above\n").lower()
if choice == "1" or choice == "fanta":
Order[DRINK] = "Fanta"
break
if choice == "2" or choice == "coke":
Order[DRINK] = "Coke"
break
if choice == "3" or choice == "pepsi":
Order[DRINK] = "Pepsi"
break
if choice == "4" or choice == "sprite":
Order[DRINK] = "Sprite"
break
This is pretty clunky, but it's most in line with what you've already got. I'd suggesting moving most of these items into lists like this, but that's a bit of a refactor for this answer. Consider the below, but really all you have to do is stick your input into an infinite loop and then break when the user gets it right.
drink_list = ['fanta', 'coke', 'pepsi', 'sprite']
if choice in drink_list:
break
Use a while
loop and break once you're satisfied:
drink = None
while drink is None:
choice = input("Please enter a drink from the menu above\n").lower()
if choice == "1" or choice == "fanta":
drink = "Fanta"
elif choice == "2" or choice == "coke":
drink = "Coke"
elif choice == "3" or choice == "pepsi":
drink = "Pepsi"
elif choice == "4" or choice == "sprite":
drink = "Sprite"
Order[DRINK] = drink
As an example, for the drink part to continually loop over until the user enters something valid:
while choice != "1" or choice != "2" or choice != "3" or choice != "4":
choice = input("Please enter a valid drink: ")
if choice == "1" or choice == "fanta":
Order[DRINK] = "Fanta"
if choice == "2" or choice == "coke":
Order[DRINK] = "Coke"
if choice == "3" or choice == "pepsi":
Order[DRINK] = "Pepsi"
if choice == "4" or choice == "sprite":
Order[DRINK] = "Sprite"
I think this would work because it would keep iterating over the loop until the condition is met. And then once the loop is completed, it will execute the code below it.