1. 程式人生 > >高階程式設計技術(Python)作業3

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

3-7 縮減名單:你剛得知新購買的餐桌無法及時送達,因此只能邀請兩位嘉賓。
- 以完成練習3-6時編寫的程式為基礎,在程式末尾新增一行程式碼,列印一條你只能邀請兩位嘉賓共進晚餐的訊息。
- 使用pop() 不斷地刪除名單中的嘉賓,直到只有兩位嘉賓為止。每次從名單中彈出一位嘉賓時,都列印一條訊息,讓該嘉賓知悉你很抱歉,無法邀請他來共進晚餐。
- 對於餘下的兩位嘉賓中的每一位,都列印一條訊息,指出他依然在受邀人之列。
- 使用del 將最後兩位嘉賓從名單中刪除,讓名單變成空的。 列印該名單,核實程式結束時名單確實是空的。

Solution:

friends = ["Tim"
, "Tom", "Pat", "Coco"] for friend in friends: print("I would like to have dinner with you, " + friend + '.') print("\nI have found a larger table to have dinner.") friends.insert(0, "Ashero") friends.insert(2, "Lily") friends.append("Faker") for friend in friends: print("I would like to have dinner with you, "
+ friend + '.') print("\nMy new table counldn't arrive in time. I can only invite 2 people.") print("Sorry, " + friends.pop() + ". I counldn't invite you to my dinner!") print("Sorry, " + friends.pop() + ". I counldn't invite you to my dinner!") print("Sorry, " + friends.pop() + ". I counldn't invite you to my dinner!"
) print("Sorry, " + friends.pop() + ". I counldn't invite you to my dinner!") print("Sorry, " + friends.pop() + ". I counldn't invite you to my dinner!\n") for friend in friends: print("You still can have dinner with me, " + friend + '.') del friends[0] del friends[0] print(friends)

Output:

I would like to have dinner with you, Tim.
I would like to have dinner with you, Tom.
I would like to have dinner with you, Pat.
I would like to have dinner with you, Coco.

I have found a larger table to have dinner.
I would like to have dinner with you, Ashero.
I would like to have dinner with you, Tim.
I would like to have dinner with you, Lily.
I would like to have dinner with you, Tom.
I would like to have dinner with you, Pat.
I would like to have dinner with you, Coco.
I would like to have dinner with you, Faker.

My new table counldn't arrive in time. I can only invite 2 people.
Sorry, Faker. I counldn't invite you to my dinner!
Sorry, Coco. I counldn't invite you to my dinner!
Sorry, Pat. I counldn't invite you to my dinner!
Sorry, Tom. I counldn't invite you to my dinner!
Sorry, Lily. I counldn't invite you to my dinner!

You still can have dinner with me, Ashero.
You still can have dinner with me, Tim.
[]

注:學習了迴圈之後應該可以將程式碼pop()的部分簡化。

3-8 放眼世界:想出至少5個你渴望去旅遊的地方。
- 將這些地方儲存在一個列表中,並確保其中的元素不是按字母順序排列的。
- 按原始排列順序列印該列表。不要考慮輸出是否整潔的問題,只管列印原始Python列表。
- 使用sorted() 按字母順序列印這個列表,同時不要修改它。
- 再次列印該列表,核實排列順序未變。
- 使用sorted() 按與字母順序相反的順序列印這個列表,同時不要修改它。
- 再次列印該列表, 核實排列順序未變。
- 使用reverse()修改列表元素的排列順序。列印該列表,核實排列順序確實變了。
- 使用reverse() 再次修改列表元素的排列順序。列印該列表,核實已恢復到原來的排列順序。
- 使用sort() 修改該列表,使其元素按字母順序排列。列印該列表,核實排列順序確實變了。
- 使用sort() 修改該列表,使其元素按與字母順序相反的順序排列。列印該列表,核實排列順序確實變了。

Solution:

places = ["Japan", "America", "England", "Canada", "Australia"]
print(places)
print(sorted(places))
print(places)
print(sorted(places, reverse = True))
print(places)
places.reverse()
print(places)
places.reverse()
print(places)
places.sort()
print(places)
places.sort(reverse = True)
print(places)

Output:

['Japan', 'America', 'England', 'Canada', 'Australia']
['America', 'Australia', 'Canada', 'England', 'Japan']
['Japan', 'America', 'England', 'Canada', 'Australia']
['Japan', 'England', 'Canada', 'Australia', 'America']
['Japan', 'America', 'England', 'Canada', 'Australia']
['Australia', 'Canada', 'England', 'America', 'Japan']
['Japan', 'America', 'England', 'Canada', 'Australia']
['America', 'Australia', 'Canada', 'England', 'Japan']
['Japan', 'England', 'Canada', 'Australia', 'America']

3-9 晚餐嘉賓:在完成練習3-4~練習3-7時編寫的程式之一中,使用len() 列印一條訊息,指出你邀請了多少位嘉賓來與你共進晚餐。

Solution:

friends = ["Tim", "Tom", "Pat", "Coco"]
for friend in friends:
    print("I would like to have dinner with you, " + friend + '.')
print("I have invited " + str(len(friends)) + " people to my dinner.")

Output:

I would like to have dinner with you, Tim.
I would like to have dinner with you, Tom.
I would like to have dinner with you, Pat.
I would like to have dinner with you, Coco.
I have invited 4 people to my dinner.

注:len(列表)的輸出是一個數字,需要用強制型別轉換str()才能和字串一起操作。