我无法解决我的代码问题。我需要循环我的代码,直到输入有效的答案

时间:2016-01-30 23:25:24

标签: 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("")
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

4 个答案:

答案 0 :(得分:0)

<强>更新

以下是代码的第一部分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循环直到该语句为真。因此,将所有代码放在循环中并在正确时满足布尔值。例如:

correct = false
while (!correct):
    var = input ("your statement")
    if var == "good":
        correct = true
#next stuff

答案 1 :(得分:0)

使用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

答案 2 :(得分:0)

作为一个例子,让饮料部分不断循环直到用户输入有效的东西:

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"

我认为这样可行,因为它会继续迭代循环直到满足条件。然后一旦循环完成,它将执行它下面的代码。

答案 3 :(得分:0)

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