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

猜你喜欢

转载自blog.csdn.net/weixin_38311046/article/details/79594223