《python编程从入门到实践》 第3章习题选做

xiaoxiao2021-02-28  50

3-1 姓名:将一些朋友的姓名存储在一个列表中,并将其命名为names。依次访问该列表中的每一个元素,从而将每个朋友的名字打印出来。

names = ['Captain America', 'Black widow', 'Spider-Man'] for name in names: print(name)

   执行结果:

Captain America Black widow Spider-Man

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

def print_list(name_list): print("Make an invitation:") for name in name_list: message = "Hello, " + name + " !Let's dinner together." print(message) dinner_name_list = [] name = input("Invite some person to eat together('q' to quit):\n") while name != 'q': dinner_name_list.append(name) name = input() print_list(dinner_name_list)

    执行结果:

Invite some person to eat together('q' to quit): Einstein Hawking Turing q Make an invitation: Hello, Einstein !Let's dinner together. Hello, Hawking !Let's dinner together. Hello, Turing !Let's dinner together.

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

#续3-4代码 print("\nSorry, Einstein have no time to attend!") print("Please choose another person to take his place:") dinner_name_list.remove("Einstein") name = input() dinner_name_list.append(name) print_list(dinner_name_list)

    执行结果:

Sorry, Einstein have no time to attend! Please choose another person to take his place: Sherlock Holmes Make an invitation: Hello, Hawking !Let's dinner together. Hello, Turing !Let's dinner together. Hello, Sherlock Holmes !Let's dinner together.

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

#续3-5代码 print("\nNow you have a bigger table.Please invite three more person") name = input() dinner_name_list.insert(0,name) name = input() dinner_name_list.insert(2,name) name = input() dinner_name_list.append(name) print_list(dinner_name_list)

    执行结果:

Now you have a bigger table.Please invite three more person Tracy McGrady Sun Yat-sen Bill Gates Make an invitation: Hello, Tracy McGrady !Let's dinner together. Hello, Hawking !Let's dinner together. Hello, Sun Yat-sen !Let's dinner together. Hello, Turing !Let's dinner together. Hello, Sherlock Holmes !Let's dinner together. Hello, Bill Gates !Let's dinner together.

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

#续3-6代码 print("\nI am so sorry.The new table can't be delivered in time") print("You can invite only two person in total") while len(dinner_name_list)!=2: del_name = dinner_name_list.pop() mes = "Sorry, " + del_name + ".We can't eat together now!" print(mes) for name in dinner_name_list: mes = name + ", we still can eat together." print(mes) print("\nNext, try to delete the list.") del dinner_name_list[1] del dinner_name_list[0] if dinner_name_list: print("Sorry, the list is still not empty.You should check the problem") else: print("Now the list is empty")

    执行结果:

I am so sorry.The new table can't be delivered in time You can invite only two person in total Sorry, Bill Gates.We can't eat together now! Sorry, Sherlock Holmes.We can't eat together now! Sorry, Turing.We can't eat together now! Sorry, Sun Yat-sen.We can't eat together now! Tracy McGrady, we still can eat together. Hawking, we still can eat together. Next, try to delete the list. Now the list is empty

        

转载请注明原文地址: https://www.6miu.com/read-2626812.html

最新回复(0)