首页 > 解决方案 > 如何停止循环,直到用户输入关键字停止?

问题描述

我的代码现在处于无限循环中,显示甜甜圈的菜单选项。我想要它,以便用户选择尽可能多的甜甜圈,直到他们输入“5”。

这是我的代码:

print("Welcome to Dino's International Doughnut Shoppe!")
name = input("Please enter your name to begin: ")

#doughnuts menu
loop = 0
while loop == 0:
    choice = 0
    while choice not in [1,2,3,4]:
        print("Please enter a valid choice from 1-4.")
        print("Please select a doughnut from the following menu: ")
        print("1. Chocolate-dipped Maple Puff ($3.50 each)")
        print("2. Strawberry Twizzler ($2.25 each)")
        print("3. Vanilla Chai Strudel ($4.05 each)")
        print("4. Honey-drizzled Lemon Dutchie ($1.99)")
        print("5. No more doughnuts.")
        choice = int(input(">"))


if choice == 1:
    chocolate = int(input("How many chocolate-dipped Maple Puff(s) would you like to purchase? "))
elif choice == 2:
    strawberry = int(input("How many Strawberry Twizzler(s) would you like to purchase? "))
elif choice == 3:
    vanilla = int(input("How many Vanilla Chai Strudel(s) would you like to purchase? "))
elif choice == 4:
    honey = int(input("How many Honey-drizzled Lemon Dutchie(s) would you like to purchase? "))
elif choice == 5:
    print(f"{name}, Here is your receipt: ")

    if choice == 1:
        print("==========================================")
        print(f"{chocolate} Chocolate Dipped Maple Puffs")
        print("==========================================")
        print(f"Total Cost: ${chocolate*3.50:.2f}")
    elif choice == 2:
        print("==========================================")
        print(f"{strawberry} Strawberry Twizzlers")
        print("==========================================")
        print(f"Total Cost: ${strawberry*2.25:.2f}")
    elif choice == 3:
        print("==========================================")
        print(f"{vanilla} Vanilla Chai Strudels")
        print("==========================================")
        print(f"Total Cost: ${vanilla*4.05:.2f}")
    elif choice == 4:
        print("==========================================")
        print(f"{honey} Honey-drizzled Lemon Dutchies")
        print("==========================================")
        print(f"Total Cost: ${honey*1.99:.2f}")

print("Thank you for shopping at Dino's International Doughnut Shoppe! Please come again!")

所以现在代码只连续显示甜甜圈菜单,但我想要它,所以当输入 5 时,它会转到代码的数学计算/结束。

标签: python

解决方案


这里有几个问题。

首先是响应选择的逻辑在您的while循环之外。这可以通过缩进整个块来解决。

其次,当用户输入 时5,条件while choice not in [1,2,3,4]:评估为True,因此提示用户再次输入有效选择。这可以通过while完全移除该内部循环来解决。

最后,在到达elif choice == 5块时,用户将看不到任何这些收据打印,因为choice是,5因此不是1,,,或。我认为您在这里的意思是, , , 或非零的计数。而且这些都应该是而不是块,因为它们彼此独立(用户可以获得一些巧克力和一些香草)。234chocolatestrawberryvanillahoneyifelif

考虑到所有这些,这里是一个重构:

print("Welcome to Dino's International Doughnut Shoppe!")
name = input("Please enter your name to begin: ")

#doughnuts menu
chocolate = strawberry = vanilla = honey = 0
done = False
while not done:
    print("Please enter a valid choice from 1-4.")
    print("Please select a doughnut from the following menu: ")
    print("1. Chocolate-dipped Maple Puff ($3.50 each)")
    print("2. Strawberry Twizzler ($2.25 each)")
    print("3. Vanilla Chai Strudel ($4.05 each)")
    print("4. Honey-drizzled Lemon Dutchie ($1.99)")
    print("5. No more doughnuts.")
    choice = int(input(">"))


    if choice == 1:
        chocolate = int(input("How many chocolate-dipped Maple Puff(s) would you like to purchase? "))
    elif choice == 2:
        strawberry = int(input("How many Strawberry Twizzler(s) would you like to purchase? "))
    elif choice == 3:
        vanilla = int(input("How many Vanilla Chai Strudel(s) would you like to purchase? "))
    elif choice == 4:
        honey = int(input("How many Honey-drizzled Lemon Dutchie(s) would you like to purchase? "))
    elif choice == 5:
        done = True
        print(f"{name}, Here is your receipt: ")

        if chocolate > 1:
            print("==========================================")
            print(f"{chocolate} Chocolate Dipped Maple Puffs")
            print("==========================================")
            print(f"Total Cost: ${chocolate*3.50:.2f}")
        if strawberry > 1:
            print("==========================================")
            print(f"{strawberry} Strawberry Twizzlers")
            print("==========================================")
            print(f"Total Cost: ${strawberry*2.25:.2f}")
        if vanilla > 1:
            print("==========================================")
            print(f"{vanilla} Vanilla Chai Strudels")
            print("==========================================")
            print(f"Total Cost: ${vanilla*4.05:.2f}")
        if honey > 1:
            print("==========================================")
            print(f"{honey} Honey-drizzled Lemon Dutchies")
            print("==========================================")
            print(f"Total Cost: ${honey*1.99:.2f}")

    print("Thank you for shopping at Dino's International Doughnut Shoppe! Please come again!")

推荐阅读