3-4?嘉宾名单:如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;然后,使用这个列表打印消息,邀请这些人来与你共进晚餐。 ?3-5 修改嘉宾名单:你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。 ·以完成练习3-4时编写的程序为基础,在程序末尾添加一条print语句,指出哪位嘉宾无法赴约。 ·修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。 ·再次打印一系列消息,向名单中的每位嘉宾发出邀请。 3-6?添加嘉宾:你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。 ·以完成练习3-4或练习3-5时编写的程序为基础,在程序末尾添加一条print语句,指出你找到了一个更大的餐桌。 ·使用insert()将一位新嘉宾添加到名单开头。 ·使用insert()将另一位新嘉宾添加到名单中间。 ·使用append()将最后一位新嘉宾添加到名单末尾。 ·打印一系列消息,向名单中的每位嘉宾发出邀请。 3-7?缩减名单:你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。 ·以完成练习3-6时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。 ·使用pop()不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐。 ·对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。 ·使用del将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。
guests=['brother','lover','father','mother','sister']
print(guests)
print(guests[0].title()+','+"I'm happy to see you at my party!")
print(guests[1].title()+','+"I'm happy to see you at my party!")
print(guests[2].title()+','+"I'm happy to see you at my party!")
print(guests[3].title()+','+"I'm happy to see you at my party!")
print(guests[4].title()+','+"I'm happy to see you at my party!")
#缺席客人
absent_guest=guests.pop(-2)
print(absent_guest)
#新客人
new_guest='baby'
guests.append(new_guest)
print(guests)
print(guests[0].title()+','+"I'm happy to see you at my party!")
print(guests[1].title()+','+"I'm happy to see you at my party!")
print(guests[2].title()+','+"I'm happy to see you at my party!")
print(guests[3].title()+','+"I'm happy to see you at my party!")
print(guests[4].title()+','+"I'm happy to see you at my party!")
#在不同位置加三位客人
guests.insert(0,'liu sir')
guests.insert(3,'ma sir')
guests.append('lu sir')
print(guests)
print(guests[0].title()+','+"I'm happy to see you at my party!")
print(guests[1].title()+','+"I'm happy to see you at my party!")
print(guests[2].title()+','+"I'm happy to see you at my party!")
print(guests[3].title()+','+"I'm happy to see you at my party!")
print(guests[4].title()+','+"I'm happy to see you at my party!")
print(guests[5].title()+','+"I'm happy to see you at my party!")
#删除客人至还剩两人
first=guests.pop(-1)
print(first+','+"I'm so sorry that we can't have a party this time")
second=guests.pop(-1)
print(second+','+"I'm so sorry that we can't have a party this time")
third=guests.pop(-1)
print(third+','+"I'm so sorry that we can't have a party this time")
forth=guests.pop(-1)
print(forth+','+"I'm so sorry that we can't have a party this time")
fifth=guests.pop(-1)
print(fifth+','+"I'm so sorry that we can't have a party this time")
sixth=guests.pop(-1)
print(sixth+','+"I'm so sorry that we can't have a party this time")
#剩下两位客人
print(guests)
print(guests[0].title()+','+"Looking forward to meeting you!")
print(guests[1].title()+','+"Looking forward to meeting you!")
#删除最后两个客人,验证打印空格
del guests[0]
del guests[0]
print(guests)
结果如下:
['brother', 'lover', 'father', 'mother', 'sister'] Brother,I'm happy to see you at my party! Lover,I'm happy to see you at my party! Father,I'm happy to see you at my party! Mother,I'm happy to see you at my party! Sister,I'm happy to see you at my party! mother ['brother', 'lover', 'father', 'sister', 'baby'] Brother,I'm happy to see you at my party! Lover,I'm happy to see you at my party! Father,I'm happy to see you at my party! Sister,I'm happy to see you at my party! Baby,I'm happy to see you at my party! ['liu sir', 'brother', 'lover', 'ma sir', 'father', 'sister', 'baby', 'lu sir'] ? ? ?? Liu Sir,I'm happy to see you at my party! Brother,I'm happy to see you at my party! Lover,I'm happy to see you at my party! Ma Sir,I'm happy to see you at my party! Father,I'm happy to see you at my party! Sister,I'm happy to see you at my party! lu sir,I'm so sorry that we can't have a party this time baby,I'm so sorry that we can't have a party this time sister,I'm so sorry that we can't have a party this time father,I'm so sorry that we can't have a party this time ma sir,I'm so sorry that we can't have a party this time lover,I'm so sorry that we can't have a party this time ['liu sir', 'brother'] Liu Sir,Looking forward to meeting you! Brother,Looking forward to meeting you! []
?
|