Question
教材中课后的练习,8-1到8-17,选一些写到你的博客上
Answer
'''
SYSU - [专选]高级编程技术 - Week4, Lecture2 HW
by Duan
2018.03.28
'''
'''
8-1 消息
'''
def display_message():
"""打印一个句子,指出本章的学习内容"""
print("I've learned how to build a function.")
display_message()
#Output:
# I've learned how to build a function.
'''
8-3 T恤
'''
def make_shirt(size, others):
"""接受一个尺码以及要印到T恤上的字样,打印一个句子"""
print("We'll make a T-shirt painted 'Hello world!' of size", size + '.')
make_shirt(size = 'M', others = 'Hello world!')
#Output:
# We'll make a T-shirt painted 'Hello world!' of size M.
'''
8-5 城市
'''
def describe_city(city, nation = 'China'):
print(city.title(), 'is in', nation.title() + '.')
cities = {'Shanghai': 'China', 'Wuhan': 'China', 'Tokyo': 'Japan'}
for city, nation in cities.items():
describe_city(city, nation)
#Output:
# Shanghai is in China.
# Wuhan is in China.
# Tokyo is in Japan.
'''
8-7 专辑
'''
def make_album(singer, album, num_of_song = ''):
if num_of_song:
description = {'Album name': album, 'Singer': singer, 'Song Number': num_of_song}
else:
description = {'Album name': album, 'Singer': singer}
return description
print(make_album('Nirvana', 'MTV unplugged In New York', 14))
print(make_album('illion', 'P.Y.L'))
print(make_album('Douwei', 'Yuxu', 12))
#Output:
# {'Album name': 'MTV unplugged In New York', 'Singer': 'Nirvana', 'Song Number': 14}
# {'Album name': 'P.Y.L', 'Singer': 'illion'}
# {'Album name': 'Yuxu', 'Singer': 'Douwei', 'Song Number': 12}
'''
8-11 不变的魔术师
'''
def show_magicians(magicians):
for magician in magicians:
print(magician)
def make_great(magicians):
for i in range(0, len(magicians)):
magicians[i] = 'the Great ' + magicians[i]
return magicians
magicians = ['Steven Frayne', 'Franz Harary', 'Jason Latimer']
changed_magicians = make_great(magicians[:])
show_magicians(magicians)
show_magicians(changed_magicians)
#Output:
# Steven Frayne
# Franz Harary
# Jason Latimer
# the Great Steven Frayne
# the Great Franz Harary
# the Great Jason Latimer
'''
8-14 汽车
'''
def make_car(manufacturer, model, **car_info):
car = {}
car['manufacturer'] = manufacturer
car['model'] = model
for key, value in car_info.items():
car[key] = value
return car
car = make_car('subaru', 'outback', color = 'blue', tow_package = True)
print(car)
#Output:
# {'manufacturer': 'subaru', 'model': 'outback', 'color': 'blue', 'tow_package': True}