Python编程:从入门到实践 第3章 列表简介 课后练习 3-4~3-7

3-4 嘉宾名单 :如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少 3 个你想邀请的人;然后,使用
这个列表打印消息,邀请这些人来与你共进晚餐。

names = ['Lisa', 'Henry', 'Jolie', 'Allen']
for name in names:
    print(name + ', I would like to invite you to have dinner with us together.')

结果:

Lisa, I would like to invite you to have dinner with us together.
Henry, I would like to invite you to have dinner with us together.
Jolie, I would like to invite you to have dinner with us together.
Allen, I would like to invite you to have dinner with us together.

3-5 修改嘉宾名单 :你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。
以完成练习 3-4 时编写的程序为基础,在程序末尾添加一条 print 语句,指出哪位嘉宾无法赴约。
修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。
再次打印一系列消息,向名单中的每位嘉宾发出邀请。

names = ['Lisa', 'Henry', 'Jolie', 'Allen']
people =names.pop(1)
names.append('Maggie')
for name in names:
    print(name + ', I would like to invite you to have dinner with us together.')
print(people + " can't have dinner with us.")

结果:

Lisa, I would like to invite you to have dinner with us together.
Jolie, I would like to invite you to have dinner with us together.
Allen, I would like to invite you to have dinner with us together.
Maggie, I would like to invite you to have dinner with us together.
Henry can't have dinner with us.


3-6 添加嘉宾 :你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。
以完成练习 3-4 或练习 3-5 时编写的程序为基础,在程序末尾添加一条 print 语句,指出你找到了一个更大的餐桌。
使用 insert() 将一位新嘉宾添加到名单开头。
使用 insert() 将另一位新嘉宾添加到名单中间。
使用 append() 将最后一位新嘉宾添加到名单末尾。
打印一系列消息,向名单中的每位嘉宾发出邀请。

names = ['Lisa', 'Henry', 'Jolie', 'Allen']
people =names.pop(1)
names.append('Maggie')
for name in names:
    print(name + ', I would like to invite you to have dinner with us together.')
print(people + " can't have dinner with us.")
print('I find a bigger table !')
names.insert(0, 'Tim')
names.insert(2, 'June')
names.append('Green')
for who in names:
    print(who + ', I would like to invite you to have dinner with us together.')

结果:

Lisa, I would like to invite you to have dinner with us together.
Jolie, I would like to invite you to have dinner with us together.
Allen, I would like to invite you to have dinner with us together.
Maggie, I would like to invite you to have dinner with us together.
Henry can't have dinner with us.
I find a bigger table !
Tim, I would like to invite you to have dinner with us together.
Lisa, I would like to invite you to have dinner with us together.
June, I would like to invite you to have dinner with us together.
Jolie, I would like to invite you to have dinner with us together.
Allen, I would like to invite you to have dinner with us together.
Maggie, I would like to invite you to have dinner with us together.
Green, I would like to invite you to have dinner with us together.


3-7 缩减名单 :你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。
以完成练习 3-6 时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。
使用 pop() 不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进
晚餐。
对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。
使用 del 将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。

names = ['Lisa', 'Henry', 'Jolie', 'Allen']
people = names.pop(1)
names.append('Maggie')
print('I have found a bigger table!')
names.insert(0, 'Tim')
names.insert(2, 'June')
names.append('Green')
for name in names:
    print(name + ', I would like to invite you to have dinner with us together.')
print('I am sorry to say that only two guests can have dinner with me.')
names1 = names.pop()
print(names1 + ", I'm sorry that I can't invite you.")
names2 = names.pop()
print(names2 + ", I'm sorry that I can't invite you.")
names3 = names.pop()
print(names3 + ", I'm sorry that I can't invite you.")
names4 = names.pop()
print(names4 + ", I'm sorry that I can't invite you.")
names5 = names.pop()
print(names5 + ", I'm sorry that I can't invite you.")
print(names[0] + ', you can come for the dinner.')
print(names[1] + ', you can come for the dinner.')
del names[0]
del names[0]
print(names)



结果:

I have found a bigger table!
Tim, I would like to invite you to have dinner with us together.
Lisa, I would like to invite you to have dinner with us together.
June, I would like to invite you to have dinner with us together.
Jolie, I would like to invite you to have dinner with us together.
Allen, I would like to invite you to have dinner with us together.
Maggie, I would like to invite you to have dinner with us together.
Green, I would like to invite you to have dinner with us together.
I am sorry to say that only two guests can have dinner with me.
Green, I'm sorry that I can't invite you.
Maggie, I'm sorry that I can't invite you.
Allen, I'm sorry that I can't invite you.
Jolie, I'm sorry that I can't invite you.
June, I'm sorry that I can't invite you.
Tim, you can come for the dinner.
Lisa, you can come for the dinner.
[]

猜你喜欢

转载自blog.csdn.net/hjk120key3/article/details/81772081
今日推荐