高級編程技術(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()才能和字符串一起操作。

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