[英]How to have the program constantly check for while statement conditions?
我正在为一个简单的食物菜单类型的东西制作这个代码,并希望将用户限制在 6 个项目。 我正在使用小于或等于 6 的 while 语句,但即使 orderitems > 6 它也会继续运行。如果 orderitems > 6,我如何让 python 停止 while 语句?
foodorders = []
drinkorders = []
orderitems=0
while orderitems <= 6:
print("SANDWHICH MENU")
print("BLT.......... 5.99")
print("FRENCH DIP .......... 7.99")
print("TURKEY CLUB .......... 4.99")
print("TRUFFLE .......... 11.99")
print("GRILLED CHEESE.......... 3.99")
moresand="yes"
while moresand=="yes":
print("Which sandwhich do you want?")
sand=str(input())
foodorders.append(sand)
orderitems=orderitems+1
print("Would you like another sandwhich?")
moresand=str(input())
else:
print("SOUP MENU")
print("FRENCH ONION .......... 5.99")
print("BROCCOLI .......... 3.99")
print("CLAM CHOWDER .......... 4.99")
print("SPLIT PEA .......... 3.99")
print("CHCIKEN NOODLE .......... 2.99")
moresoup="yes"
while moresoup=="yes":
print("Which soup do you want?")
soup=str(input())
foodorders.append(soup)
orderitems=orderitems+1
print("Would you like another soup?")
moresoup=str(input())
else:
print("DRINK MENU")
print("SMALL .......... 1.99")
print("MEDIUM .......... 2.99")
print("LARGE .......... 3.99")
moresoda="yes"
while moresoda=="yes":
print("Which soda do you want?")
soda=str(input())
drinkorders.append(soda)
orderitems=orderitems+1
print("Would you like another soda?")
moresoda=str(input())
else:
print("Thank you for shopping!")
print("Your food orders are:")
for foodorder in foodorders:
print("A",foodorder)
print("Your drink orders are:")
for drinkorder in drinkorders:
print("A",drinkorder,"Soda")
else:
print("Looks like that's all of our stock, sorry about that! Have a great rest of your day!")
print("Your final food orders are:")
for foodorder in foodorders:
print("A",foodorder)
print("Your drink orders are:")
for drinkorder in drinkorders:
print("A",drinkorder,"Soda")
不,你不能有一个程序来不断检查 while 语句。 每个循环只会检查一次。
您可以使用 if 语句重新检查 while 循环中的条件。
您可以使用 break 来停止 while 循环:
foodorders = []
drinkorders = []
orderitems=0
while orderitems < 7: # [1, 2, 3, 4, 5, 6] -> 6 orders
print("SANDWHICH MENU")
print("BLT.......... 5.99")
print("FRENCH DIP .......... 7.99")
print("TURKEY CLUB .......... 4.99")
print("TRUFFLE .......... 11.99")
print("GRILLED CHEESE.......... 3.99")
moresand="yes"
while moresand=="yes":
print("Which sandwhich do you want?")
sand=str(input())
foodorders.append(sand)
orderitems=orderitems+1
if orderitems >= 6: break
print("Would you like another sandwhich?")
moresand=str(input())
else:
print("SOUP MENU")
print("FRENCH ONION .......... 5.99")
print("BROCCOLI .......... 3.99")
print("CLAM CHOWDER .......... 4.99")
print("SPLIT PEA .......... 3.99")
print("CHCIKEN NOODLE .......... 2.99")
moresoup="yes"
while moresoup=="yes":
print("Which soup do you want?")
soup=str(input())
foodorders.append(soup)
orderitems=orderitems+1
if orderitems >= 6: break
print("Would you like another soup?")
moresoup=str(input())
else:
print("DRINK MENU")
print("SMALL .......... 1.99")
print("MEDIUM .......... 2.99")
print("LARGE .......... 3.99")
moresoda="yes"
while moresoda=="yes":
print("Which soda do you want?")
soda=str(input())
drinkorders.append(soda)
orderitems=orderitems+1
if orderitems >= 6: break
print("Would you like another soda?")
moresoda=str(input())
else:
print("Thank you for shopping!")
print("Your food orders are:")
for foodorder in foodorders:
print("A",foodorder)
print("Your drink orders are:")
for drinkorder in drinkorders:
print("A",drinkorder,"Soda")
else:
print("Looks like that's all of our stock, sorry about that! Have a great rest of your day!")
print("Your final food orders are:")
for foodorder in foodorders:
print("A",foodorder)
要做的简单事情是更改while moresand=="yes":
更改为while moresand=="yes" and orderitems <= 5:
对 moreSoup 和 moreSoda 执行相同操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.