你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。
□以完成练习3-6时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。
□使用pop()不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐。
□对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。
□使用del将最后两位嘉宾从名单中删除,让名单变成空的,打印该名单,核实程序结束时名单确实是空的。
代码:
names=["Iron Man","Spider Man","Captain","Ant-Man"]
print(names[0]+","+"do you have time for dinner with me this weenkend?")
print(names[1]+","+"do you have time for dinner with me this weenkend?")
print(names[2]+","+"do you have time for with me this weenkend?")
print(names[3]+","+"do you have time for dinner with me this weenkend?")
print(names[3]+" can't be invited to dinner for some unexpected business.")
print("\nI just found a bigger table for gathering together to have dinner this weekend.")
names.insert(0,'Black Widow')
names.insert(2,'Thor')
names.append('Hulk')
print(names)
print("\nDo you have time for dinner with me this weenkend"+","+names[0]+"?")
print("\nDo you have time for dinner with me this weenkend"+","+names[1]+"?")
print("\nDo you have time for dinner with me this weenkend"+","+names[2]+"?")
print("\nDo you have time for dinner with me this weenkend"+","+names[3]+"?")
print("\nDo you have time for dinner with me this weenkend"+","+names[4]+"?")
print("\nDo you have time for dinner with me this weenkend"+","+names[5]+"?")
print("\nDo you have time for dinner with me this weenkend"+","+names[6]+"?")
print("\nThis weekend I can only invite two people to dinner.")
apology=names.pop()
print("\nI'm really so sorry,"+apology+"."+"Because I don't find a big table, I cannot invite you to dinner this weekend.We can postpone the gathering until the end of the month if you have time.")
print(names)
apology=names.pop()
print("\nI'm really so sorry,"+apology+"."+"Because I don't find a big table, I cannot invite you to dinner this weekend.We can postpone the gathering until the end of the month if you have time.")
print(names)
apology=names.pop()
print("\nI'm really so sorry,"+apology+"."+"Because I don't find a big table, I cannot invite you to dinner this weekend.We can postpone the gathering until the end of the month if you have time.")
print(names)
apology=names.pop()
print("\nI'm really so sorry,"+apology+"."+"Because I don't find a big table, I cannot invite you to dinner this weekend.We can postpone the gathering until the end of the month if you have time.")
print(names)
apology=names.pop()
print("\nI'm really so sorry,"+apology+"."+"Because I don't find a big table, I cannot invite you to dinner this weekend.We can postpone the gathering until the end of the month if you have time.")
print(names)
print("\nSince I couldn't find a big table, the number of people was reduced to two. "+names [0]+","+"you are still among the invitees. Looking forward to having dinner with you on the weekend.")
print("\nSince I couldn't find a big table, the number of people was reduced to two. "+names [1]+","+"you are still among the invitees. Looking forward to having dinner with you on the weekend.")
del names[0]
print(names)
del names[0]
print(names)
运行结果: