高階程式設計技術(Python)作業7

2021-08-18 01:11:06 字數 3755 閱讀 4903

書上寫了sublime無法執行使用者互動的**,事實上只要安裝乙個repl就可以進行使用者互動了。但是repl並沒有辦法進行死迴圈的跳出處理,所以一旦**出現死迴圈,sublime就會失去響應,只能關閉sublime。而不使用repl正常執行python就可以使用ctrl + c中斷死迴圈但是又不能進行使用者互動。因此如果要用sublime編寫大規模的**並執行,最好還是在cmd的環境下執行。

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

solution:

sandwich_orders = ["hotdog", "salad", "tuna"]

finished_sandwiches =

while(sandwich_orders):

sandwich = sandwich_orders.pop()

print("i made your " + sandwich +" sandwich.")

print("\nthese are sandwiches finished.")

for finished_sandwich in finished_sandwiches:

print("\t" + finished_sandwich.title() + " sandwich")

output:

i made your tuna sandwich.

i made your salad sandwich.

i made your hotdog sandwich.

these are sandwiches finished.

tuna sandwich

salad sandwich

hotdog sandwich

7-9 五香菸薰牛肉(pastrami)賣完了:使用為完成練習7-8而建立的列表sandwich_orders,並確保』pastrami』 在其中至少出現了三次。在程式開頭附近新增這樣的**:列印一條訊息,指出熟食店的五香菸薰牛肉賣完了;再使用乙個while 迴圈將列表sandwich_orders中的』pastrami』 都刪除。確認最終的列表finished_sandwiches 中不包含』pastrami』。

solution:

sandwich_orders = ["hotdog", "pastrami", "salad", 

"pastrami", "tuna", "pastrami"]

finished_sandwiches =

print("all the pastrami is sold out!\n")

while

"pastrami"

in sandwich_orders:

sandwich_orders.remove("pastrami")

while(sandwich_orders):

sandwich = sandwich_orders.pop()

print("i made your " + sandwich +" sandwich.")

print("\nthese are sandwiches finished.")

for finished_sandwich in finished_sandwiches:

print("\t" + finished_sandwich.title() + " sandwich")

if"pastrami"

in finished_sandwiches:

print("\nthere must be something wrong.")

else:

print("\nthere are no pastrami sandwiches.")

output:

all the pastrami is sold out!

i made your tuna sandwich.

i made your salad sandwich.

i made your hotdog sandwich.

these are sandwiches finished.

tuna sandwich

salad sandwich

hotdog sandwich

there are no pastrami sandwiches.

7-10 夢想的度假勝地:編寫乙個程式,調查使用者夢想的度假勝地。使用類似於「if you could visit one place in the world, where would you go?」的提示,並編寫乙個列印調查結果的**塊。

solution:

polling_active = true

responses = {}

while polling_active:

name = input("what is your name? ")

sentence = "if you could visit one place in the world,"

sentence += "\nwhere would you go ? "

place = input(sentence)

responses[name] = place

while

true:

repeat = input("finished? y/n\n")

if (repeat == "y"):

polling_active = false

break

elif(repeat == "n"):

print("next one.\n")

break

else:

print("wrong input. please input again.\n")

print("this is the result.")

for name, place in responses.items():

print(name.title() + " would like to " + place.title() + ".")

output:

if you could visit one place in

the world,

where would you go ? tokyo

finished? y/n

nnext one.

what is your name? ashero

if you could visit one place in

the world,

where would you go ? paris

finished? y/n

djdwrong input. please input again.

finished? y/n

ythis is

theresult.

tim would like to tokyo.

ashero would like to paris.

注:書上的例項沒有判斷使用者如果亂輸入的情況,實際問題中應該考慮這個問題。flag active的主要用途就是用來處理迴圈中套迴圈的跳出判斷

高階程式設計技術作業 7

題目描述 使用乙個字典來儲存一些人喜歡的數字。請想5個人的名字,並將這些名字用作字典中 的鍵 想出每個人喜歡的乙個數字,並將這些數字作為值儲存在字典中。列印每個人的名字和喜歡 的數字。展示 dic for name,number in dic.items print name str number ...

高階程式設計技術(Python)作業5

5 9 處理沒有使用者的情形 在為完成練習5 8編寫的程式中,新增一條if 語句,檢查使用者名稱列表是否為空。如果為空,就列印訊息 we need to find some users 刪除列表中的所有使用者名稱,確定將列印正確的訊息。solution users if users for user...

高階程式設計技術作業 5

題目描述 使用乙個for迴圈列印數字1 20 包含 展示 for number in range 1,21 print number input null output 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 題目描述 通過給函式rang...