Python作业(3.1-3.9)
3-1 姓名: 将一些朋友的姓名存储在一个列表中, 并将其命名为names 。 依次访问该列表中的每个元素, 从而将每个朋友的姓名都打印出来。
names=['XiaoYing','HanQian','XuYiDan']
print(names[0])
print(names[1])
print(names[2])
3-2
问候语: 继续使用练习
3-1
中的列表, 但不打印每个朋友的姓名, 而为每人打印一条消息。 每条消息都包含相同的问候语, 但抬头为相应朋友的姓名。
names=['XiaoYing','HanQian','XuYiDan']
for friend in names:
print(friend+",how are you"+"?")
3-3 自己的列表: 想想你喜欢的通勤方式, 如骑摩托车或开汽车, 并创建一个包含多种通勤方式的列表。 根据该列表打印一系列有关这些通勤方式的宣言, 如“I would like to own a Honda motorcycle”。
commuting=['plane','steamship','car']
print("I will travel America by "+ commuting[0])
print("I like seeing the sight of sea in" +commuting[1])
print("I want to have my own "+ commuting[2])
3-4 嘉宾名单 : 如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的) , 你会邀请哪些人? 请创建一个列表, 其中包含至少3个你想邀请的人; 然后, 使用这个列表打印消息, 邀请这些人来与你共进晚餐
guests=['Ma Longyuan','Zhou Qingyue','Xu Yidan']
print("I want to invite all of you to have dinner,"+guests[0]+","+guests[1]+",and"+guests[2]+".")
3-5
修改嘉宾名单 : 你刚得知有位嘉宾无法赴约, 因此需要另外邀请一位嘉宾。以完成练习
3-4
时编写的程序为基础, 在程序末尾添加一条
print
语句, 指出哪位嘉宾无法赴约。修改嘉宾名单, 将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。guests=['Ma Longyuan','Zhou Qingyue','Xu Yidan']
print("I want to invite all of you to have dinner,"+guests[0]+","+guests[1]+",and"+guests[2]+".")
print("However,"+guests[2]+" have no time.")
guests[2]='Guo Haiyi'
for guest in guests:
print(guest+",I hope you can have dinner with me.")
3-6
添加嘉宾 : 你刚找到了一个更大的餐桌, 可容纳更多的嘉宾。 请想想你还想邀请哪三位嘉宾。以完成练习
3-4
或练习
3-5
时编写的程序为基础, 在程序末尾添加一条
print
语句, 指出你找到了一个更大的餐桌。使用
insert()
将一位新嘉宾添加到名单开头。使用
insert()
将另一位新嘉宾添加到名单中间。使用
append()
将最后一位新嘉宾添加到名单末尾。打印一系列消息, 向名单中的每位嘉宾发出邀请。
guests=['Ma Longyuan','Zhou Qingyue','Xu Yidan']
guests.insert(0,'Guo Haiyi')
guests.insert(1,'Yu Jiayi')
guests.append('Yang Qianye')
for guest in guests:
print(guest+",I would like to invite you to a meal!")
print("So sorry for that I just can invite two friends to have a meal with me!")
for guest in guests[2:]:
people_delete=guests.pop()
print("Sorry,"+people_delete+",I can't invite you to dinner.")
for guest in guests[0:]:
people_cur=guest
print(people_cur+",You're still invited!")
del guests[1]
del guests[0]
print(guests)
3-8
放眼世界 : 想出至少
5
个你渴望去旅游的地方。citys=['New York','Paris','Istanbul','Melbourne','Roman']
print(citys)
print(sorted(citys))
print(citys)
print(sorted(citys,reverse=True))
print(citys)
citys.reverse()
print(citys)
citys.reverse()
print(citys)
citys.sort()
print(citys)
citys.sort(reverse=True)
print(citys
guests=['Ma Longyuan','Zhou Qingyue','Xu Yidan']
print(len(guests))