Python编程 从入门到实践 动手试一试 第3章答案

3-2

names = ["alice","bob","lily","jack","tony"]
for i in range(5):
    print(names[i].title()+",早上好!")

3-4

print("3-4")
dinner_list = ["alice","bob","lily"]
for i in range(3):
    print(dinner_list[i].title()+",欢迎和我共进晚餐~")

3-5

print("\n\n\n3-5")    
absent = "bob"
dinner_list.remove(absent)
print("非常遗憾"+absent.title()+"不能参加今天的晚宴……")
print("那么现在能来的嘉宾为:")
for i in range(2):
    print(dinner_list[i].title(),end="、")

print("\n\n替换缺席嘉宾,重新发出邀请:")
dinner_list.insert(1,"bill")
for i in range(3):
    print(dinner_list[i].title()+",欢迎和我共进晚餐~")

3-6

print("\n\n\n3-6")
print("餐桌变大了,新增嘉宾.")
print("1.使用insert将angle添到名单开头")
print("2.使用insert将cendy添到名单中间")
print("3.使用append将添tony到名单末尾\n")
dinner_list.insert(0,"angle")
dinner_list.insert(int(len(dinner_list)/2),"cendy")
dinner_list.append("tony")

for i in range(6):
    print(dinner_list[i].title()+",欢迎和我共进晚餐~")

3-7

print("\n\n\n3-7")
print("最后只能邀请两位.")
while len(dinner_list) > 2:
    ls = dinner_list.pop()
    print(ls.title()+",很抱歉今晚不能邀请你了~")

print("\n")
for i in range(2):
    print(dinner_list[i]+",继续邀请你参加晚宴~")

del dinner_list[1]
del dinner_list[0]
if len(dinner_list) == 0:
    print("\n已删除最后两名嘉宾,列表为空!")

你可能感兴趣的:(Python,Python,《Python编程,从入门到实践》)