python - 如何停止循环,直到用户输入关键字停止?
问题描述
我的代码现在处于无限循环中,显示甜甜圈的菜单选项。我想要它,以便用户选择尽可能多的甜甜圈,直到他们输入“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 时,它会转到代码的数学计算/结束。
解决方案
这里有几个问题。
首先是响应选择的逻辑在您的while
循环之外。这可以通过缩进整个块来解决。
其次,当用户输入 时5
,条件while choice not in [1,2,3,4]:
评估为True
,因此提示用户再次输入有效选择。这可以通过while
完全移除该内部循环来解决。
最后,在到达elif choice == 5
块时,用户将看不到任何这些收据打印,因为choice
是,5
因此不是1
,,,或。我认为您在这里的意思是, , , 或非零的计数。而且这些都应该是而不是块,因为它们彼此独立(用户可以获得一些巧克力和一些香草)。2
3
4
chocolate
strawberry
vanilla
honey
if
elif
考虑到所有这些,这里是一个重构:
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!")
推荐阅读
- c++ - 在找到二叉树的高度时这两个语句的区别?
- angular - 无法使用 ionic-http-plugin 在卡上显示 api 数据
- android-instant-apps - 如何在多模块动态特性应用程序中进行通信
- javascript - 如何在javascript中将大量html内容复制到剪贴板而不会超时
- angular - 如何在角度中查找属性(在 HTML 代码中绑定)源库?
- react-native - 我们如何在基于类的组件中将导航 4 的切换导航器替换为导航 5(不使用 redux)?
- postgresql - 根据条件连接多行的列值
- azure - azure容器组中的多个容器部署
- perl - 如何通过数组哈希中的值获取哈希的键
- java - 如何异步运行@ParameterizedTest