1. 程式人生 > >Python 從入門到實踐 7-8 課後習題

Python 從入門到實踐 7-8 課後習題

7.8

熟食店:建立一個名為sandwich_orders 的列表,在其中包含各種三明治的名
字;再建立一個名為finished_sandwiches 的空列表。遍歷列表sandwich_orders,對於
其中的每種三明治,都列印一條訊息,如I made your tuna sandwich,並將其移到列表
finished_sandwiches。所有三明治都製作好後,列印一條訊息,將這些三明治列出來。

sandwich_orders = ['Pice','Noy','Pop']
finished_sandwichers=[]
while sandwich_orders:
    finished_sandwicher = sandwich_orders.pop()
    print
("I made your " + str(finished_sandwicher) +" sandwich.") finished_sandwichers.append(finished_sandwicher) print(str(finished_sandwichers)) for finished_sandwicher in finished_sandwichers: print(finished_sandwicher.title())
7.9五香菸薰牛肉(pastrami)賣完了:使用為完成練習7-8 而建立的列表
sandwich_orders,並確保'pastrami'在其中至少出現了三次。在程式開頭附近新增這樣
的程式碼:列印一條訊息,指出熟食店的五香菸薰牛肉賣完了;再使用一個while 迴圈將
列表sandwich_orders 中的'pastrami'都刪除。確認最終的列表finished_sandwiches 中
不包含'pastrami'。
sandwich_orders = ['tofu'
,'Pice','pastrami','Noy','pastrami','Pop','pastrami'] print("The pastrami has been sold.") while 'pastrami' in sandwich_orders: sandwich_orders.remove('pastrami') print(sandwich_orders)
7.10夢想的度假勝地:編寫一個程式,調查使用者夢想的度假勝地。使用類似於“If
you could visit one place in the world, where would you go?”的提示,並編寫一個列印調
查結果的程式碼塊。
place=[]
print
("If you could visit one place in the world, where would you go?") #設定標誌位 polling_active = True while polling_active: new_place = input("\n Please input the place?") place.append(new_place) repeat = input("Would you want to input another place?(Y/N)") if repeat == 'N': polling_active = False for i in place: print("The " + i + " you want to go.")

問題: 7.10輸出時候,最後剛開始用的直接列印,程式碼為(print("The " + str(place) " + " you want to go. "))

輸出為 The ['Sydney']  you want to go.