您的位置:首页 > 编程语言

【高级编程技术】【作业】【第二周】【1】

2018-03-13 13:03 218 查看

教材第3章课后练习

3-1 姓名

names = ['alice', 'bob', 'charlie']
print(names[0].title())
print(names[1].title())
print(names[-1].title())


3-2 问候语

names = ['alice', 'bob', 'charlie']
print('Hello,', names[0].title()+'!')
print('Hello,', names[1].title()+'!')
print('Hello,', names[-1].title()+'!')


3-3 自己的列表

commuting_tools = ['car', 'bicycle', 'bus']
for commuting_tool in commuting_tools:
print('I would like to own a', commuting_tool+'.')


3-4 嘉宾名单

guests = ['alice', 'bob', 'charlie']
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')


3-5 修改嘉宾名单

guests = ['alice', 'bob', 'charlie']
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
print(guests[-1].title(), 'cannot keep the appointment.')
guests[-1] = 'david'
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')


3-6 添加嘉宾

guests = ['alice', 'bob', 'charlie']
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
print(guests[-1].title(), 'cannot keep the appointment.')
guests[-1] = 'david'
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
print('I have found a bigger dinner table.')
guests.insert(0, 'eric')
guests.insert(2, 'fred')
guests.append('gay')
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')


3-7 缩减名单

guests = ['alice', 'bob', 'charlie']
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
print(guests[-1].title(), 'cannot keep the appointment.')
guests[-1] = 'david'
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
print('I have found a bigger dinner table.')
guests.insert(0, 'eric')
guests.insert(2, 'fred')
guests.append('gay')
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
print('I can only invite two people to have dinner together.')
while len(guests) > 2:
print('Sorry,', guests.pop().title()+', I feel sorry of being unable to invite you.')
for guest in guests:
print(guest.title()+', you are still in the invitation list.')
del guests[0]
del guests[0]
print(guests)


3-8 放眼世界

attractions = [
'Dyrholaey',
'Nordkapp',
'Aurora Sky Station',
'Arnarstapi',
'OIA',
'Cliffs of Moher',
'Mount Fuji',
'Salar de Uyuni',
]
print(attractions)
print(sorted(attractions))
print(attractions)
print(sorted(attractions, reverse=True))
print(attractions)
attractions.reverse()
print(attractions)
attractions.reverse()
print(attractions)
attractions.sort()
print(attractions)
attractions.sort(reverse=True)
print(attractions)


3-9 晚餐嘉宾

guests = ['alice', 'bob', 'charlie']
for guest in guests:
print('Dear', guest.title()+', I would like to invite you to have dinner together.')
print(len(guests), 'guests are invited.')


3-10 尝试使用各个函数

things = [
'mountain',
'river',
'country',
'city',
'language',
'anything I like',
]
print(things)

things.append('append')
print(things)

things.insert(3, 'insert')
print(things)

del things[3]
print(things)

things.pop()
print(things)

things.remove('anything I like')
print(things)

print(sorted(things))
print(things)

things.sort()
print(things)

things.reverse()
print(things)

print(len(things))


3-11 有意引发错误

lists = [i+1 for i in range(1, 5)]
# print(lists[4]) # 索引错误
print(lists[3])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: