[英]Python loop not breaking out
我希望當我的isQuit
變量為"yes"
並且客戶變為False
時,整個循環將退出。 但是,當前循環似乎要重復,直到order_item
在menu_items
為止。
您能幫我更改代碼,以便在用戶想要退出時停止循環嗎?
def order(menu):
FISH_CHIPS_PRICES = menu
menu_items = []
for item in FISH_CHIPS_PRICES:
menu_items.append(item.lower())
orders = []
customer = True
while customer:
orders.append({})
for item in FISH_CHIPS_PRICES:
orders[-1][item] = 0
while True:
while True:
order_item = input("What do you want to buy?")
if order_item.lower() not in menu_items:
print("Item:", order_item, "not available")
isQuit = input("Do you want to quit: yes / no:")
if isQuit == "yes":
customer = False
else:
break
由於兩個無限的while循環包含在isQuit
變量中,因此您會看到此行為。 break
循環只會停止立即封閉的循環。
一種可能的解決方案是,當isQuit
為"yes"
時立即return
,這將退出整個order()
函數。
# ...
isQuit = input("Do you want to quit: yes / no:")
if isQuit == "yes":
return
另一種可能的解決方案是刪除外部無限while循環,因為它似乎沒有任何功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.