python⼊门作业编程题-Python编程:从⼊门到实践——【作
业】——第三章(列表)...
第三章作业
3-1姓名: 将⼀些朋友的姓名存储在⼀个列表中, 并将其命名为names 。 依次访问该列表中的每个元素, 从⽽将每个朋友的姓名都打印出来。
3-2问候语: 继续使⽤练习 3-1中的列表, 但不打印每个朋友的姓名, ⽽为每⼈打印⼀条消息。 每条消息都包含相同的问候语, 但抬头为相应朋友的姓名。
3-3⾃ ⼰的列表: 想想你喜欢的通勤⽅式, 如骑摩托车或开汽车, 并创建⼀个包含多种通勤⽅式的列表。 根据该列表打印⼀系列有关这些通勤⽅式的宣⾔, 如“
I would like to own a Honda motorcycle”。
答:
names = ['zhaing','caofu','caohui']print(names[0])print(names[1])print(names[2])
输出;
zhaing
caofu
caohui
3-2
names = ['zhaing','caofu','caohui']
message= 'Hello' + names[0].title() + '.'
print(message)android硬件开发
输出:
Hello Zhaing.
还能这样
names = ['qiqi','qanliang','mingliang','peng']for x in range(0,4):print("Hello," + names[x].title() + "⼤吉⼤利!")
输出:Hello,Qiqi⼤吉⼤利!
Hello,Qanliang⼤吉⼤利!
Hello,Mingliang⼤吉⼤利!
Hello,Peng⼤吉⼤利!
3-3
trip_mode = ['⼩黄车','摩拜','暴龙兽','⼤宝剑']for x in range(0,4):print("我今天想骑" + trip_mode[x] + "去抓⽪卡丘!")
输出:
我今天想骑⼩黄车去抓⽪卡丘!
我今天想骑摩拜去抓⽪卡丘!
python基础代码练习我今天想骑暴龙兽去抓⽪卡丘!
我今天想骑⼤宝剑去抓⽪卡丘!
3-4嘉宾名单 : 如果你可以邀请任何⼈⼀起共进晚餐(⽆论是在世的还是故去的) , 你会邀请哪些⼈? 请创建⼀个列表, 其中包含⾄少3个你想邀请的⼈; 然后, 使⽤
这个列表打印消息, 邀请这些⼈来与你共进晚餐。
3-5修改嘉宾名单 : 你刚得知有位嘉宾⽆法赴约, 因此需要另外邀请⼀位嘉宾。
以完成练习 3-4时编写的程序为基础, 在程序末尾添加⼀条print 语句, 指出哪位嘉宾⽆法赴约。
修改嘉宾名单, 将⽆法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。
再次打印⼀系列消息, 向名单中的每位嘉宾发出邀请。
3-6添加嘉宾 : 你刚到了⼀个更⼤的餐桌, 可容纳更多的嘉宾。 请想想你还想邀请哪三位嘉宾。
以完成练习 3-4或练习 3-5时编写的程序为基础, 在程序末尾添加⼀条print 语句, 指出你到了⼀个更⼤的餐桌。
使⽤insert( ) 将⼀位新嘉宾添加到名单开头。
使⽤insert( ) 将另⼀位新嘉宾添加到名单中间。
使⽤append( ) 将最后⼀位新嘉宾添加到名单末尾。
打印⼀系列消息, 向名单中的每位嘉宾发出邀请。
3-7缩减名单 : 你刚得知新购买的餐桌⽆法及时送达, 因此只能邀请两位嘉宾。
以完成练习 3-6时编写的程序为基础, 在程序末尾添加⼀⾏代码, 打印⼀条你只能邀请两位嘉宾共进晚餐的消息。
使⽤pop( ) 不断地删除名单中的嘉宾, 直到只有两位嘉宾为⽌。 每次从名单中弹出⼀位嘉宾时, 都打印⼀条消息, 让该嘉宾知悉你很抱歉, ⽆法邀请他来共进
晚餐。
对于余下的两位嘉宾中的每⼀位, 都打印⼀条消息, 指出他依然在受邀⼈之列。
使⽤del 将最后两位嘉宾从名单中删除, 让名单变成空的。 打印该名单, 核实程序结束时名单确实是
空的。
答:
3-4
list = ['zhangsan','lisi','wangshi','liu']for x in range(0,4):print('I would like invite' + list[x].title() + 'to have a dineer')
输出:
I would like invite Zhangsanto have a dineer
I would like invite Lisito have a dineer
I would like invite Wangshito have a dineer
I would like invite Liuto have a dineer
另⼀种⽅法:
list = ['zhangsan','lisi','wangshi','liu']
message= 'I would like invite ' + list[0].title() + 'to have a dineer'
print(message)
fileattribute输出;css文字两端对齐属性
I would like invite Zhangsan to have a dineer
3-5
list = ['zhangsan','lisi','wangshi']for x in range(0,3):print('I would like invite' + list[x].title() + 'to have a dineer')print(list[1] + '⽆法赴约。')
list[1] = 'caofu'#新增加⼈
for x in range(0,3):print('I would like invite' + list[x].title() + 'to have a dineer')
输出:
I would like invite Zhangsan to have a dineer
I would like invite Lisi to have a dineer
I would like invite Wangshi to have a dineer
lisi⽆法赴约。
I would like invite Zhangsan to have a dineer
I would like invite Caofu to have a dineer
I would like invite Wangshi to have a dineer
3-6
list = ['zhangsan','lisi','wangshi']for x in range(0,3):print('I would like invite' + list[x].title() + 'to have a dineer')print(' Haha~,I found a bigger table, I can invite more people! ')#这个之前提到的知识给忘了,要空格,要断⾏
list.insert(0,'梅西')
list.insert(3,'詹姆斯')
list.insert(6,'⼩罗')#插⼊新⼈
for x in range(0,6):print('I would like invite' + list[x].title() + 'to have a dineer')
输出;
I would like invite Zhangsan to have a dineer
I would like invite Lisi to have a dineer
I would like invite Wangshi to have a dineer
Haha~,I found a bigger table, I can invite more people!
I would like invite 梅西 to have a dineer
I would like invite Zhangsan to have a dineer
I would like invite Lisi to have a dineer
I would like invite 詹姆斯 to have a dineer
I would like invite Wangshi to have a dineer
I would like invite ⼩罗 to have a dineer
3-7
1 list = ['zhangsan','lisi','wangshi']
2 for x in range(0,3):
3 print('I would like invite' + list[x].title() + 'to have a dineer')
4 print(' Haha~,I found a bigger table, I can invite more people! ')#这个之前提到的知识给忘了,要空格,要断⾏
5 list.insert(0,'梅西')
6 list.insert(3,'詹姆斯')
7 list.insert(6,'⼩罗')#插⼊新⼈
8 for x in range(0,6):9 print('I would like invite' + list[x].title() + 'to have a dineer')10 print(' Sorry~,I only can invite two people, the table can not arrived in time ! ')11 popped_list =list.pop()12 print(list)1
3 print(popped_list + '很抱歉,我不能与你进餐了')14 popped_list =list.pop()15 print(popped_list + '很抱歉,我不能与你进餐了')16 popped_list =list.pop()17
print(popped_list + '很抱歉,我不能与你进餐了')18 popped_list =list.pop()19 print(popped_list + '很抱歉,我不能与你进餐了')20 print(list)21 for x in range(0,2):#(0,1就不⾏啊,只有这个可以) 注意看前⾯的for rang 也是(0,3)
22 print('Dear' + list[x] + ',我依然邀请你来与我共进晚餐!')23 del list[1] #del列表应从后往前!
24 dellist[0]25 print(list)26
27 输出:28 I would like invite Zhangsan to have a dineer29 I would like invite Lisi to have a dineer30 I would like invite Wangshi to have a dineer31
32 Haha~,I found a bigger table, I can invite more people!33
34 I would like invite 梅西 to have a dineer35 I would like invite Zhangsan to have a dineer36 I would like invite Lisi to have a dineer37 I would like invite 詹姆斯 to have a dineer38 I would like invite Wangshi to have a dineer39 I would like invite ⼩罗 to have a dineer40
41 Sorry~,I only can invite two people, the table can not arrived intime !42
43 ['梅西', 'zhangsan', 'lisi', '詹姆斯', 'wangshi']44 ⼩罗很抱歉,我不能与你进餐了45 wangshi很抱歉,我不能与你进餐了46 詹姆斯很抱歉,我不能与你进餐了47 lisi很抱歉,我不能与你进餐了48 ['梅西', 'zhangsan']49 Dear 梅西,我依然邀请你来与我共进晚餐!50 Dear zhangsan,我依然邀请你来与我共进晚餐!51 []openstack manila
题⽬难啊
我写的太复杂了,参考下别⼈⽤for循环的
1 #3-7 缩减名单
2 names =[]
3 names.append('新垣结⾐')
4 names.append('奥黛丽赫本')
5 names.append('迪丽热巴')
6 for x in range(0,3):7
print('Dear' + names[x] + ',Can you come to my dinner?')8
9 print(' ' + names[1] + '⽆法赴约。 ')10
11 names[1] = ('赵丽颖') #改名单
12 for x in range(0,3):13 print('Dear' + names[x] + ',Can you come to my dinner?')14
15 print(' Haha~,I found a bigger table, I can invite more people! ')16 names.insert(0,'紫霞')17 names.insert(2,'静⾹')18 names.append('提莫')19
20 for x inrange(0,len(names)):21 print('Dear' + names[x] + ',Can you come to my dinner?')22
23 print(' Oh! GouRiDe快递没给我把桌⼦送来,我只能邀请两位了(┬_┬)! ')24
25 X =len(names)26 for x in range(0,X-2):27 popped_name =names.pop()28 print('实在抱歉,' + popped_name +'我不能与你共进晚餐了!')29
30 for x inrange(0,len(names)):31 print('Dear' + names[x] + ',我依然邀请你来与我共进晚餐!')32
33 del names[1] #del列表应从后往前!
34 delnames[0]35 print(names)
View Code
3-8放眼世界 : 想出⾄少5个你渴望去旅游的地⽅。
将这些地⽅存储在⼀个列表中, 并确保其中的元素不是按字母顺序排列的。
按原始排列顺序打印该列表。 不要考虑输出是否整洁的问题, 只管打印原始Python列表。
使⽤sorted( ) 按字母顺序打印这个列表, 同时不要修改它。
再次打印该列表, 核实排列顺序未变。
使⽤sorted( ) 按与字母顺序相反的顺序打印这个列表, 同时不要修改它。
再次打印该列表, 核实排列顺序未变。
使⽤reverse( ) 修改列表元素的排列顺序。 打印该列表, 核实排列顺序确实变了。
使⽤reverse( ) 再次修改列表元素的排列顺序。 打印该列表, 核实已恢复到原来的排列顺序。
使⽤sort( ) 修改该列表, 使其元素按字母顺序排列。 打印该列表, 核实排列顺序确实变了。
使⽤sort( ) 修改该列表, 使其元素按与字母顺序相反的顺序排列。 打印该列表, 核实排列顺序确实变了。
3-9晚餐嘉宾 : 在完成练习 3-4~练习 3-7时编写的程序之⼀中, 使⽤len( ) 打印⼀条消息, 指出你邀请了多少位嘉宾来与你共进晚餐。
3-10尝试使⽤各个函数 : 想想可存储到列表中的东西, 如⼭岳、 河流、 国家、 城市、 语⾔或你喜欢的任何东西。 编写⼀个程序, 在其中创建⼀个包含这些元素的列
表, 然后, 对于本章介绍的每个函数, 都⾄少使⽤⼀次来处理这个列表。
3-8
place = ['chongqing','chengdu','xiamen','kuning','shenzhen']print(place)print(sorted(place))print(place)
#核实排列顺序未变。
print(sorted(place,reverse=True))#注意是与字母顺序相反的顺序
print(place)#核实排列顺序未变。
网站开发与设计教程place.sort()print(place)
place.sort(reverse=True)print(place)
输出:
['chongqing', 'chengdu', 'xiamen', 'kuning', 'shenzhen']
['chengdu', 'chongqing', 'kuning', 'shenzhen', 'xiamen']
['chongqing', 'chengdu', 'xiamen', 'kuning', 'shenzhen']
['xiamen', 'shenzhen', 'kuning', 'chongqing', 'chengdu']
['chongqing', 'chengdu', 'xiamen', 'kuning', 'shenzhen']
['shenzhen', 'kuning', 'xiamen', 'chengdu', 'chongqing']
['chongqing', 'chengdu', 'xiamen', 'kuning', 'shenzhen']
['chengdu', 'chongqing', 'kuning', 'shenzhen', 'xiamen']
['xiamen', 'shenzhen', 'kuning', 'chongqing', 'chengdu']
3-9
list = ['zhangsan','lisi','wangshi']for x in range(0,3):print('I would like invite' + list[x].title() + 'to have a dineer')print(' Haha~,I found a bigger table, I can invite more people! ')#这个之前提到的知识给忘了,要空格,要断⾏
list.insert(0,'梅西')
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论