第三章课后作业

时间:2021-06-10 16:36:22

Chapter Three

3-1 姓名

names = ["Wu Yanbing", "Wu Dechong", "Xu Jing"]
for name in names:
print(name)

3-2 问候语

names = ["Wu Yanbing", "Wu Dechong", "Xu Jing"]
for name in names:
print("Welcome! My friend " + name + ".")

3-3 自己的列表

vehicles = ["bicycle", "car", "shared bike", "motorcycle"]
for vehicle in vehicles:
print("I would like to own a " + vehicle + ".")

3-4 嘉宾名单

guests = ["Yamato", "Fubuki", "Agaki", "Abukuma"]
for guest in guests:
print(guest + ", would you like to have dinner with me?")

3-5 修改嘉宾名单

guests = ["Yamato", "Fubuki", "Agaki", "Abukuma"]
for guest in guests:
print(guest + ", would you like to have dinner with me?")
print()
print("Fubuki is busy with poi, so she could not come to dinner")
print(" andShimakaze would come in present of her")
print()
guests[1] = "Shimakaze"
for guest in guests:
print(guest + ", would you like to have dinner with me?")

3-6 添加嘉宾

guests = ["Yamato", "Fubuki", "Agaki", "Abukuma"]
for guest in guests:
print(guest + ", would you like to have dinner with me?")
print()
print("I find a bigger table and could invite more guests.")
print()
guests.insert(0, "Shigure")
guests.insert(2, "Yudachi")
guests.append("Kawakaze")
for guest in guests:
print(guest + ", would you like to have dinner with me?")

3-7 缩减名单

guests = ["Yamato", "Fubuki", "Agaki", "Abukuma"]
for guest in guests:
print(guest + ", would you like to have dinner with me?")
print()
print("I find a bigger table and could invite more guests.")
print()
guests.insert(0, "Shigure")
guests.insert(2, "Yudachi")
guests.append("Kawakaze")
for guest in guests:
print(guest + ", would you like to have dinner with me?")
print()
print("Sorry, the table could not arrive on time, so I can only invite two guests")
print()
length = len(guests)
for index in range(0, length - 2):
print(guests.pop() + ", sorry, I could not have dinner with you.")
for guest in guests:
print(guest + ", you are still invited for dinner")
del(guests[0])
del(guests[0])
print(guests)

3-8 放眼世界

places = ["New York", "Lendon", "Tokyo", "Paris", "Dubai"]
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)

3-9 晚餐嘉宾

guests = ["Yamato", "Fubuki", "Agaki", "Abukuma"]
for guest in guests:
print(guest + ", would you like to have dinner with me?")
print()
print("Fubuki is busy with poi, so she could not come to dinner")
print(" andShimakaze would come in present of her")
print()
guests[1] = "Shimakaze"
for guest in guests:
print(guest + ", would you like to have dinner with me?")
print("I have invited " + str(len(guests)) + " guests to dinner.")

3-10 尝试使用各个函数

languages = ["C", "Java", "C++", "C#", "Python", "Virtual Basic"]
print(languages)
languages[2] = "Swift"
print(languages)
languages.append("C++")
print(languages)
del(languages[-1])
print(languages)
languages.insert(-1, "C++")
print(languages)
languages.pop()
print(languages)
languages.remove("C#")
print(languages)
print(sorted(languages))
print(languages)
languages.reverse()
print(languages)
languages.reverse()
print(languages)
languages.sort()
print(languages)
print(len(languages))

3-11 有意引发错误

guests = ["Yamato", "Fubuki", "Agaki", "Abukuma"]
for guest in guests:
print(guest + ", would you like to have dinner with me?")
# print(guests[4]) -- Error Sentence to be modified, list index out of range