第三章课后作业

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值