我正在为简单的食物菜单类型的东西制作此代码,并希望将用户限制在6个项目。我使用while语句与lessthan或等于6,但它仍然运行,即使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")
答案 0 :(得分:0)
不,你不能有一个程序来不断检查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)
答案 1 :(得分:0)
要做的简单事情就是改变
while moresand=="yes":
到while moresand=="yes" and orderitems <= 5:
为moreSoup和moreSoda做同样的事。