iT邦幫忙

0

【點餐系統】break無法成功結束迴圈

https://ithelp.ithome.com.tw/upload/images/20210427/20137026bnIEPN3UDU.png
https://ithelp.ithome.com.tw/upload/images/20210427/201370269rVJNWREvj.png
https://ithelp.ithome.com.tw/upload/images/20210427/201370264GjPYlXTVC.png
menu_list =[
{"id":1, "name":"1.漢堡","price":60,"colaries":294.9,"$":"元","kcal":"大卡"},
{"id":2, "name":"2.米漢堡","price":80,"colaries":217.7,"$":"元","kcal":"大卡"},
{"id":3, "name":"3.薯條","price":30,"colaries":311.9,"$":"元","kcal":"大卡"},
{"id":4, "name":"4.沙拉","price":45,"colaries":62,"$":"元","kcal":"大卡"},
{"id":5, "name":"5.可樂","price":20,"colaries":37.5,"$":"元","kcal":"大卡"},
{"id":6, "name":"6.紅茶","price":15,"colaries":1,"$":"元","kcal":"大卡"}]
order_list = []
print('==========歡迎光臨本餐廳,祝您用餐愉快==========')
print('今日菜單:')
for menu in menu_list:
print(menu.get('name'),menu.get('price'),menu.get('$'),menu.get('colaries'),menu.get('kcal'))
while True:
print('='*50)
print('1.點餐\n2.取消點餐\n3.確認菜單\n4.結帳')
serve = int(input("請選擇服務:"))
if serve == 1:
while True:
menu_add = input("請輸入菜名編號或者輸入X結束點餐:")
if menu_add != "X":
for i in menu_list:
if i.get('id')== int(menu_add):
order_list.append(i)
else:
print('==========已完成點餐=========')
bill = 0
for order in order_list:
print(order.get('name'),order.get('price'),order.get('$'),order.get('colaries'),order.get('kcal'))
bill += int(order.get('price'))
print(' 小計:{}元'.format(bill))
break
elif serve == 2:
menu_del = input("請輸入要取消之菜名:")
order_list.remove(order)
print('==========已完成點餐=========')
bill = 0
for order in order_list:
print(order.get('name'),order.get('price'),order.get('$'),order.get('colaries'),order.get('kcal'))
bill += int(order.get('price'))
print(' 小計:{}元'.format(bill))
elif serve == 3:
print('==========已完成點餐=========')
bill = 0
for order in order_list:
print(order.get('name'),order.get('price'),order.get('$'),order.get('colaries'),order.get('kcal'))
bill += int(order.get('price'))
print(' 小計:{}元'.format(bill))
elif serve == 4:
print('==========您的消費明細==========')
bill = 0
for order in order_list:
print(order.get('name'),order.get('price'),order.get('$'))
bill += int(order.get('price'))
print(' 總計:{}元'.format(bill))
print('==========謝謝光臨,歡迎再來==========')
break
輸入X依舊無法break
謝謝大家...

一個`break`只能跳出一個迴圈,動動腦想一想該怎麼辦。我絕不會說是用旗標。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
一級屠豬士
iT邦大師 1 級 ‧ 2021-04-28 05:56:50
while True:
    menu_add = input("請輸入菜名編號或者輸入X結束點餐:")
if menu_add != "X":

先不討論 break 的部分, 這裡 if 開始的區塊部分, 先確認一下是否縮排正確? 是不是在
while loop 的範圍裡面.
另外,你可以把程式碼貼好的,貼成那樣,減少幫你看程式碼的機率.

看更多先前的回應...收起先前的回應...

平常看隨手貼上來的程式碼看多了
對歪七扭八的程式碼早已見怪不怪

原來是縮排的原因
我還一直納悶「就算跳不出 while 為什麼不去 else」
/images/emoticon/emoticon25.gif

還是小雨大火眼金睛
/images/emoticon/emoticon12.gif/images/emoticon/emoticon12.gif/images/emoticon/emoticon12.gif

最近都老花眼了.....

已經很有誠意地貼上圖片版和純文字版的程式碼
可就是不用隔壁的 code block 框起來
/images/emoticon/emoticon10.gif

https://ithelp.ithome.com.tw/upload/images/20210428/20001787SHjYkMDw83.png

希望有那麼一天
介面可以改成這樣
https://ithelp.ithome.com.tw/upload/images/20210428/20001787ApgRenBVP8.png

是啊,再加個友善提示,好好貼程式碼,比較容易得到答案.
不過這年頭更多的是貼張糊掉小張的照片....
相對來說,這已經是比較好的了.

/images/emoticon/emoticon12.gif

1

我只提示一點

break 只會跳出目前所在的區塊。如FOR跳出FOR。WHILE跳出WHILE

我要發表回答

立即登入回答