我无法验证我的代码.我需要循环我的代码,直到输入有效答案为止 [英] I am having trouble vaildating my code.I need my code to loop untill a valid answer is inputted
问题描述
我正在努力处理这段Python代码.问题是,当用户输入错误时,我需要我的代码保持循环,直到他们输入有效答案为止. 代码应该这样工作:提示用户选择饮料,然后是美食,然后是菜式.此后,程序将显示用户想要的顺序.
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 code is supposed to work: User is prompted to choose a drink, then a cuisine, then a dish. After this, the program displays the order the user wanted.
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")
choice = input("Please enter a drink from the menu above\n").lower()
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"
print ("You have chosen: " + Order[DRINK])
print("")
print("Now, let me get your food order, if any.")
answer = input("Do you want any food (Y/N)? ").lower()
if answer in Yesses:
answer = input("Do you want Italian,Indian or Chinese food?\n").lower()
if answer == "indian":
answer = input("Do you want Curry or Onion Bhaji?\n").lower()
if answer == "curry":
Order[FOOD] = "Curry"
answer = input("With your curry, do you want Rice or Naan?\n").lower()
if answer == "rice":
Order.append("- with rice")
if answer == "naan":
Order.append("- with naan")
if answer == "onion bhaji" or answer == "bhaji":
Order[FOOD] = "Onion Bhaji"
answer = input("With your bhaji, do you want Chili or Peppers?\n").lower()
if answer == "chili":
Order.append("- with chili")
if answer == "peppers":
Order.append("- with peppers")
if answer == "chinese":
answer = input("Do you want Chicken Wings or Noodles?\n").lower()
if answer == "chicken wings" or answer == "wings":
Order[FOOD] = "Chicken Wings"
answer = input("With your wings, do you want Chips or Red Peppers?\n").lower()
if answer == "chips":
Order.append("- with Chips")
if answer == "red peppers" or answer == "peppers":
Order.append("- with Red Peppers")
if answer == "noodles":
Order[FOOD] = "Noodles"
answer = input("With your noodles, do you want Meatballs or Chicken?\n").lower()
if answer == "meatballs":
Order.append("- with meatballs")
if answer == "chicken":
Order.append("- with chicken")
if answer == "italian":
answer = input("Do you want Pasta or Noodles?\n").lower()
if answer == "pasta" or answer == "Pasta":
Order[FOOD] = "Pasta"
answer = input("With your Pasta, do you want Vegetarian Toppings or meat toppings?\n").lower()
if answer == "vegetarian Toppings":
Order.append("- with Vegetarian Toppings")
if answer == "meat toppings" or answer == "meat":
Order.append("- with Meat toppings")
if answer == "pizza":
Order[FOOD] = Pizza
answer = input("With your Pizza, do you want Grilled chicken or Chicken?\n").lower()
if answer == "Grilled chicken":
Order.append("- Grilled chicken")
if answer == "seasonal vegetables":
Order.append("- seasonal vegetables")
try:
if Order[2]:
print("You have ordered the following. Your order number is 294")
print("")
print(" ", Order[DRINK])
print(" ", Order[FOOD])
except:
pass
try:
if Order[2]:
print(" ", Order[2])
except:
print("")
print("No food or drink?! Get out!")
try:
if Order[2]:
print("""
Your order should arrive within 10-50 minutes. If you wish to cancel,
please contact us at 077 3475 8675309. Thank you for your patronage!
""")
except:
pass
推荐答案
更新:
这是代码的第一部分,带有while (condition):
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])
原始答案:
因此,如果使用的是input()
,则可以执行while循环直到语句为真.因此,将所有代码放入循环中,并在正确时实现布尔值.例如:
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
这篇关于我无法验证我的代码.我需要循环我的代码,直到输入有效答案为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!