《Python编程:从入门到实践》第三章3.2节课后作业

本文代码是在jupyter中实现的,仅为了自我督促学习python之用。

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

本题主要是学会如何构建一个列表,以及打印列表内容。

代码:

names = ['Tom', 'Rayne', 'Christian', 'Chris']
print(names[0] + ',' + " I want to invite you to dinner.")
print(names[1] + ',' + " I want to invite you to dinner.")
print(names[2] + ',' + " I want to invite you to dinner.")
print(names[3] + ',' + " I want to invite you to dinner.")

运行结果:

Tom, I want to invite you to dinner.
Rayne, I want to invite you to dinner.
Christian, I want to invite you to dinner.
Chris, I want to invite you to dinner.

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

本题主要是学会替换列表里的内容。

代码:

names = ['Tom', 'Rayne', 'Christian', 'Chris']
print(names)
print("However" +','+ names[-1] + " was unable to keep the appointment due to temporary incidents.")
names[-1] = 'Blair'
print(names)
print(names[0] + ',' + " I want to invite you to dinner.")
print(names[1] + ',' + " I want to invite you to dinner.")
print(names[2] + ',' + " I want to invite you to dinner.")
print(names[3] + ',' + " I want to invite you to dinner.")

运行结果:

['Tom', 'Rayne', 'Christian', 'Chris']
However,Chris was unable to keep the appointment due to temporary incidents.
['Tom', 'Rayne', 'Christian', 'Blair']
Tom, I want to invite you to dinner.
Rayne, I want to invite you to dinner.
Christian, I want to invite you to dinner.
Blair, I want to invite you to dinner.

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

本题主要是学会往列表里添加内容。
 方法 append() 将新元素添加到了列表末尾。
 方法 insert() 通过指定新元素的索引和值,可在列表的任何位置添加新元素。

代码:

names = ['Tom', 'Rayne', 'Christian', 'Chris']
print(names)
print("However" +','+ names[-1] + " was unable to keep the appointment due to temporary incidents.")
names[-1] = 'Blair'
print(names)
print("To tell you the good news, I found a bigger table so that I can invite a few more friends to dinner with you.")
names.insert(0, 'Jenny')
print(names)
names.insert(3, 'Li Ming')
print(names)
names.append('Jam') # 方法 append() 将元素 'ducati' 添加到了列表末尾
print(names)
print(names[0] + ',' + " I want to invite you to dinner.")
print(names[1] + ',' + " I want to invite you to dinner.")
print(names[2] + ',' + " I want to invite you to dinner.")
print(names[3] + ',' + " I want to invite you to dinner.")
print(names[4] + ',' + " I want to invite you to dinner.")
print(names[5] + ',' + " I want to invite you to dinner.")
print(names[6] + ',' + " I want to invite you to dinner.")

运行结果:

['Tom', 'Rayne', 'Christian', 'Chris']
However,Chris was unable to keep the appointment due to temporary incidents.
['Tom', 'Rayne', 'Christian', 'Blair']
To tell you the good news, I found a bigger table so that I can invite a few more friends to dinner with you.
['Jenny', 'Tom', 'Rayne', 'Christian', 'Blair']
['Jenny', 'Tom', 'Rayne', 'Li Ming', 'Christian', 'Blair']
['Jenny', 'Tom', 'Rayne', 'Li Ming', 'Christian', 'Blair', 'Jam']
Jenny, I want to invite you to dinner.
Tom, I want to invite you to dinner.
Rayne, I want to invite you to dinner.
Li Ming, I want to invite you to dinner.
Christian, I want to invite you to dinner.
Blair, I want to invite you to dinner.
Jam, I want to invite you to dinner.

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

本题主要是学会对列表内的某些元素进行删减。
 只要知道元素在列表中的索引,del可以对列表内任意位置的元素进行删减,被删除的元素不可被调用。
 方法 pop() 可删除列表末尾的元素,并让你能够接着使用它。在pop()括号中指定要删除的元素的索引,就可以对删除列表中任何位置的元素进行删除。
 另外,使用 remove() 从列表中删除元素时,也可接着使用它的值。

代码:

names = ['Jenny', 'Tom', 'Rayne', 'Li Ming', 'Christian', 'Blair', 'Jam']
print('I’m sorry, \n because the table I ordered was not delivered on time, \n I can only invite two people to dinner at the moment, \n and the others will invite again in the next day.')
Second_invitation_1 = names.pop()
Second_invitation_2 = names.pop()
Second_invitation_3 = names.pop()
Second_invitation_4 = names.pop()
Second_invitation_5 = names.pop()
print(Second_invitation_1 + ', ' + "I'm extremely sorry, I'll make an appointment another day.")
print(Second_invitation_2 + ', ' + "I'm extremely sorry, I'll make an appointment another day.")
print(Second_invitation_3 + ', ' + "I'm extremely sorry, I'll make an appointment another day.")
print(Second_invitation_4 + ', ' + "I'm extremely sorry, I'll make an appointment another day.")
print(Second_invitation_5 + ', ' + "I'm extremely sorry, I'll make an appointment another day.")
print(names)
print(names[0] + ', '"Please be on time for the appointment!")
print(names[1] + ', '"Please be on time for the appointment!")
del names[1]
del names[0]
print(names)

运行结果:

I’m sorry, 
 because the table I ordered was not delivered on time, 
 I can only invite two people to dinner at the moment, 
 and the others will invite again in the next day.
Jam, I'm extremely sorry, I'll make an appointment another day.
Blair, I'm extremely sorry, I'll make an appointment another day.
Christian, I'm extremely sorry, I'll make an appointment another day.
Li Ming, I'm extremely sorry, I'll make an appointment another day.
Rayne, I'm extremely sorry, I'll make an appointment another day.
['Jenny', 'Tom']
Jenny, Please be on time for the appointment!
Tom, Please be on time for the appointment!
[]
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页