第二周作业——第三章动手试一试

3-2 问候语: 问候语: 继续使用练习3-1中的列表,但不打印每个朋友的姓名,而为每人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。
names = ['jack','marry','tom','lily']
print(names[0]+', hello!')
print(names[1]+', hello!')
print(names[2]+', hello!')
print(names[3]+', hello!')


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

names = ['jack','marry','tom','lily']
print(names[0]+', would you like to have dinner with me?')
print(names[1]+', would you like to have dinner with me?')
print(names[2]+', would you like to have dinner with me?')
print(names[3]+', would you like to have dinner with me?')
print(names[2]+', has no time to come for dinner.')
names[2] = 'rose'
print(names[0]+', would you like to have dinner with me?')
print(names[1]+', would you like to have dinner with me?')
print(names[2]+', would you like to have dinner with me?')
print(names[3]+', would you like to have dinner with me?')



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


names = ['jack','marry','tom','lily']
print(names[0]+', would you like to have dinner with me?')
print(names[1]+', would you like to have dinner with me?')
print(names[2]+', would you like to have dinner with me?')
print(names[3]+', would you like to have dinner with me?')

print('I find a bigger table ,so I can invite more friends')

names.insert(0,'rose')

names.insert(3,'emma')

names.append('mason')


print(names[0]+', would you like to have dinner with me?')
print(names[1]+', would you like to have dinner with me?')
print(names[2]+', would you like to have dinner with me?')
print(names[3]+', would you like to have dinner with me?')
print(names[4]+', would you like to have dinner with me?')
print(names[5]+', would you like to have dinner with me?')
print(names[6]+', would you like to have dinner with me?')


3-8 放眼世界 放眼世界 :想出至少5个你渴望去旅游的地方。
将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的。
按原始排列顺序打印该列表。不要考虑输出是否整洁的问题,只管打印原始Python列表。 使用sorted() 按字母顺序打印这个列表,同时不要修改它。
再次打印该列表,核实排列顺序未变。 使用sorted() 按与字母顺序相反的顺序打印这个列表,同时不要修改它。
再次打印该列表,核实排列顺序未变。 使用reverse() 修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。 使用reverse() 再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序。 使用sort() 修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了。 使用sort() 修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了。

places = ['ChengTu','Beijing','Sian','Lahsa','Tibet']

print(places)

print(sorted(places))

print(places)

print(sorted(places,reverse=True ))

print(places)

places.reverse()

print(places)

places.reverse()

print(places)

places.sort()

print(places)

places.sort(reverse=True)

print(places)


3-9 晚餐嘉宾 晚餐嘉宾 :在完成练习3-4~练习3-7时编写的程序之一中,使用len() 打印一条消息,指出你邀请了多少位嘉宾来与你共进晚餐。

names = ['jack','marry','tom','lily']

print(len(names))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值