高級編程技術(Python)作業7

書上寫了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.")
    finished_sandwiches.append(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.")
    finished_sandwiches.append(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:

What is your name? tim
If you could visit one place in the world,
where would you go ? tokyo
Finished? Y/N
N
Next one.

What is your name? ashero
If you could visit one place in the world,
where would you go ? paris
Finished? Y/N
djd
Wrong input. Please input again.

Finished? Y/N
Y
This is the result.
Tim would like to Tokyo.
Ashero would like to Paris.

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章