-2

我更改了步骤的值,但程序一遍又一遍地询问我的三明治输入。它应该更改 step 的值,以便程序可以退出第一个 while 循环并进入第二个 while 循环,但由于某种原因,第一个循环不断重复。

def main():
    order = 0
    step = 0
    total = 0
    while step == 0:
        print ("Welcome to Jeremy's Meat Haven, please pick one drink, one salad, and one sandwitch.")
        print ("Please select a sandwitch by inputting 1, 2, or 3")
        print ("(1) Hamburger  -$1.00") # Print the first option on the menu, and its price, for the user
        print ("(2) Cheeseburger -$1.50") # Print the second option on the menu, and its price, for the user
        print ("(3) Lambburger -$2.00") # Print the third option on the menu, and its price, for the user

        order =  input("What would you like to order? (enter number): ") # Prompt the user for the number on the menu of the item they want to order

        if order == 1:
            total = total + 1
            step = step + 1

        elif order == 2:
            total = total + 1.5
            step = step + 1

        elif order == 3:
            total = total + 2
            step = step + 1

        elif order != 1 or 2 or 3:
            print ("please enter a valid value of 1, 2, or 3")

        while step == 1:
         print ("Please select a drink by inputting 1, 2, or 3")

         print ("(1) milkshake  -$1.00") # Print the first option on the menu, and its price, for the user

         print ("(2) coke  -$1.00") # Print the second option on the menu, and its price, for the user

         print ("(1) lemonade  -$1.00") # Print the third option on the menu, and its price, for the user



    main()
4

2 回答 2

2

当您在此处获得商品编号时:

order =  input("What would you like to order? (enter number): ") # Prompt the user for the number on the menu of the item they want to order

订单是一个字符串。然后根据整数对其进行测试:

if order == 1:   # Not going to be True since order will be '1', '2' or '3'

因此,改为针对字符串进行测试:

if order == '1':

或订购一个int:

order = int(...)

此外,您没有看到有关未输入 1、2 或 3 的打印错误,因为您的布尔语句需要工作:

elif order != 1 or 2 or 3:

这将评估为 True,因为if 2if 3都是 True。尝试:

elif order not in ('1', '2', '3')

于 2013-11-11T20:32:30.063 回答
0

您需要在第二个 while 语句中更改 step 的值。现在,当您输入它时,它将永远循环。

while step == 1:

    print ("Please select a drink by inputting 1, 2, or 3")

    print ("(1) milkshake  -$1.00") # Print the first option on the menu, and its price, for the user

    print ("(2) coke  -$1.00") # Print the second option on the menu, and its price, for the user

    print ("(1) lemonade  -$1.00") # Print the third option on the menu, and its price, for the user

    #ask the user to do something and change the value of step

此外,您可以更改

step = step + 1

step += 1

它做同样的事情,但更容易阅读并且更pythonic。

于 2013-11-11T20:42:34.720 回答