Python 我在输入代码时遇到问题。我需要我的代码循环,直到输入有效答案
我正在努力处理这段Python代码。问题是,当用户输入错误时,我需要我的代码保持循环,直到他们输入有效答案。 这就是代码的工作原理:提示用户选择饮料,然后选择菜肴,然后选择菜肴。之后,程序显示用户想要的顺序Python 我在输入代码时遇到问题。我需要我的代码循环,直到输入有效答案,python,validation,loops,Python,Validation,Loops,我正在努力处理这段Python代码。问题是,当用户输入错误时,我需要我的代码保持循环,直到他们输入有效答案。 这就是代码的工作原理:提示用户选择饮料,然后选择菜肴,然后选择菜肴。之后,程序显示用户想要的顺序 Order = [ 'No drink', 'No food' ] Yesses = [ 'y', 'yes', 'yep', 'okay', 'sure' ] DRINK = 0 FOOD = 1 print("Welcome to Hungary house!") print("")
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(条件):
原始答案:
因此,如果它使用的是input()
,在语句为真之前,您可以不必执行while循环。因此,将所有代码放在一个循环中,并在正确时实现布尔值。例如:
correct = false
while (!correct):
var = input ("your statement")
if var == "good":
correct = true
#next stuff
更新: 这是代码的第一部分,带有
while(条件):
原始答案:
因此,如果它使用的是input()
,在语句为真之前,您可以不必执行while循环。因此,将所有代码放在一个循环中,并在正确时实现布尔值。例如:
correct = false
while (!correct):
var = input ("your statement")
if var == "good":
correct = true
#next stuff
使用
while
循环并在您满意后中断:
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
使用
while
循环并在您满意后中断:
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
例如,要使饮料部分持续循环,直到用户输入有效的内容:
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"
我认为这是可行的,因为它会在循环中不断迭代,直到满足条件为止。循环完成后,它将执行下面的代码。例如,让饮料部分不断循环,直到用户输入有效的内容:
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"
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
我认为这是可行的,因为它会在循环中不断迭代,直到满足条件为止。一旦循环完成,它将执行下面的代码
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
这是相当笨重,但它最符合你已经有了。我建议将这些项目中的大部分移动到类似这样的列表中,但对于这个答案来说,这有点像重构。考虑下面的内容,但是你真正要做的就是把你的输入放在一个无限循环中,然后在用户正确的时候中断它。
drink_list = ['fanta', 'coke', 'pepsi', 'sprite']
if choice in drink_list:
break
这是相当笨重,但它最符合你已经有了。我建议将这些项目中的大部分移动到类似这样的列表中,但对于这个答案来说,这有点像重构。考虑下面的内容,但是你真正要做的就是把你的输入放在一个无限循环中,然后在用户正确的时候中断它。
drink_list = ['fanta', 'coke', 'pepsi', 'sprite']
if choice in drink_list:
break
我认为你应该使用argparse而不是手动滚动一个有状态的解析器。我认为你应该使用argparse而不是手动滚动一个有状态的解析器。是的,现在远离计算机,在移动设备上漫游,我很快就会格式化你的问题@rdowg我刚刚用一个准确有效的代码更新了答案,让我知道它是否对你方有效。@jarrod roberson-同意,我之所以与参考资料进行比较,只是为了让询问者能够看到参考资料。尤其是在
中,而这确实是一个非常糟糕的主意。我已经更新了答案,以反映最优化的代码。您是否推荐其他优化?我认为:if(choice==(“1”或“fanta”):
也是一个更好的解决方案,但对于初学者来说,这可能不起作用。你需要确定editor@iSkore你能给我举一个菜单顺序的例子吗?我应该如何设置它?没有操作符,只不。是的,现在远离计算机,在移动设备上漫游,我会很快格式化你的问题@rdowg我刚刚用一个准确有效的代码更新了答案,让我知道它是否对你方有效。@jarrod roberson-同意,我之所以与参考资料进行比较,只是为了让询问者能够看到参考资料。尤其是在中,而这确实是一个非常糟糕的主意。我已经更新了答案,以反映最优化的代码。您是否推荐其他优化?我认为:if(choice==(“1”或“fanta”):
也是一个更好的解决方案,但对于初学者来说,这可能不起作用。你需要确定editor@iSkore你能给我举一个菜单顺序的例子吗?我应该如何设置它?没有运算符,仅不。如果用户输入“fanta”,这不会继续循环吗?@Bink您能给我举一个用餐选项的示例吗。所以你需要添加更多的条件<代码>或选择!=回答时“芬达”
“印第安人”或答案!=“中文”还是答案!=“意大利语”
然后你需要再次询问答案的值。如果用户输入“fanta”,这不会继续循环吗?@Bink你能给我举一个用餐选项的例子吗。所以你需要添加更多的条件<代码>或选择!=回答时“芬达”
“印第安人”或答案!=“中文”还是答案!=“意大利语”
然后你需要再次询问答案的值。我输入了一个随机数,看看它是否有效,它不起作用代码刚刚移动了它应该再问一次问题这是要点,但是饮料在顶部设置为0,所以我不认为这个条件会飞起来?哦,我以为顺序是一本字典。我输入了一个随机数,看看它是否有效,它不起作用代码刚刚移动了它应该再问一次问题这是要点,但是饮料在顶部被设置为0,所以我不认为这个条件会飞起来?哦,我以为顺序是一本字典。我会解决的。我应该在哪里输入底部位@RobertRodkeyThe底部位是建议的重构的一部分,以使其更干净。如果您将项目存储在一个列表中,则更容易检查用户输入是否在该列表中。清楚地说,一个有效的答案是顶部代码和顶部代码。我应该在哪里输入底部位@RobertRodkeyThe底部位是建议的重构的一部分,以使其更干净。如果您将项目存储在一个列表中,则更容易检查用户输入是否在该列表中。明确地说,一个正在工作的answ