Python编程 从入门到实践 练习3-4~3-7

3-4 嘉宾名单

guest_list = ['alice', 'bob', 'carmen', ]
print(guest_list[0].title() + ',please come and have dinner with me.')
print(guest_list[1].title() + ',please come and have dinner with me.')
print(guest_list[2].title() + ',please come and have dinner with me.')

Python编程 从入门到实践 练习3-4~3-7_第1张图片

3-5 修改嘉宾名单

#修改嘉宾名单
guest_list = ['alice', 'bob', 'carmen', ]
unable_guest = guest_list.pop(1)
new_guest = 'beer'
guest_list.insert(1,new_guest)
print(guest_list[0].title() + ',please come and have dinner with me.')
print(guest_list[1].title() + ',please come and have dinner with me.')
print(guest_list[2].title() + ',please come and have dinner with me.')
print(unable_guest.title() + ' unable to attend.')

Python编程 从入门到实践 练习3-4~3-7_第2张图片

3-6 添加嘉宾

#修改嘉宾名单
#添加嘉宾
guests = ['alice', 'bob', 'carmen', ]
unable_guest = guests.pop(1)
new_guest = 'beer'
guests.insert(1,new_guest)
guests.insert(0,'david')
guests.insert(2,'Eric')
guests.append('ford')
print(guests[0].title() + ',please come and have dinner with me.')
print(guests[1].title() + ',please come and have dinner with me.')
print(guests[2].title() + ',please come and have dinner with me.')
print(guests[3].title() + ',please come and have dinner with me.')
print(guests[4].title() + ',please come and have dinner with me.')
print(guests[5].title() + ',please come and have dinner with me.')
print(unable_guest.title() + ' unable to attend.')
print('I find a bigger table.')

Python编程 从入门到实践 练习3-4~3-7_第3张图片

3-7 缩减名单

#修改嘉宾名单
#添加嘉宾
#缩减名单
guests = ['alice', 'bob', 'carmen', ]
unable_guest = guests.pop(1)
new_guest = 'beer'
guests.insert(1,new_guest)
guests.insert(0,'david')
guests.insert(2,'Eric')
guests.append('ford')
print(guests[0].title() + ',please come and have dinner with me.')
print(guests[1].title() + ',please come and have dinner with me.')
print(guests[2].title() + ',please come and have dinner with me.')
print(unable_guest.title() + ' unable to attend.')
print('I find a bigger table.')
print(guests[3].title() + ',please come and have dinner with me.')
print(guests[4].title() + ',please come and have dinner with me.')
print(guests[5].title() + ',please come and have dinner with me.')
print(guests.pop().title() + ",sorry can't invite you to dinner.")
print(guests.pop().title() + ",sorry can't invite you to dinner.")
print(guests.pop().title() + ",sorry can't invite you to dinner.")
print(guests.pop().title() + ",sorry can't invite you to dinner.")
print(guests[0].title() + ' is still invited.')
print(guests[1].title() + ' is still invited.')
print('I can only invite two guests to come and have dinner with me.')
del guests[1]
del guests[0]
print(guests)

Python编程 从入门到实践 练习3-4~3-7_第4张图片

你可能感兴趣的:(python编程从入门到实践)