第八章
8-2
def display_message(chapter):
"""简单的语句"""
print("本章学习 " + chapter)
display_message('函数')
本章学习 函数
def favorite_book(title):
"""tushu"""
print("woxihuaned " + title + ".")
favorite_book('python')
woxihuaned python.
8-3
def make_shirt(chima, ziyang):
"""T恤"""
print("T恤de chima shi :" + chima)
print("T恤" + chima + "de ziyangshi " + ziyang)
make_shirt('xl','python')
make_shirt(chima='xl',ziyang='pthon')
8-4
def make_shirt(ziyang='LOVE',chima='XXL' ):
"""T恤"""
print("T恤de chima shi :" + chima)
print("T恤" + chima + "de ziyangshi " + ziyang)
make_shirt()
make_shirt(chima='xl')
make_shirt(ziyang='abc')
8-5
def describe_city(name,ciaty_country='zhongguo'):
"""chengshi"""
print(name + " in " + ciaty_country)
describe_city('jiaxing')
describe_city('sichuan')
describe_city('nowyork',ciaty_country='meiguo')
8-6
def city_cpuntry(name, country):
""""""
full = name + ',' + country
return full.title()
message = city_cpuntry('jiaxing', 'zhongguo')
print(message)
8-7
def make_album(name,album,number=''):
"""fanhuizidain"""
albums = {'name_a': name,'album_a': album}
if number:
albums['number'] = number
return albums
mussage = make_album('dengziqi','xihuanni',number=2)
print(mussage)
{'name_a': 'dengziqi', 'album_a': 'xihuanni', 'number': 2}
8-8
def make_album(name,album):
"""fanhuizidain"""
albums = {'name_a': name,'album_a': album}
return albums
while True:
print("\nplease tell me your love song: ")
print("(enter 'q' at any time to quit)")
f_name = input("name: ")
if f_name == 'q':
break
l_labum = input("album: ")
if l_labum == 'q':
break
mussage = make_album(f_name,l_labum)
print(mussage)
please tell me your love song:
(enter 'q' at any time to quit)
name: dengziqi
albumxihuani
please tell me your love song:
(enter 'q' at any time to quit)
name: q
{'name_a': 'q', 'album_a': 'xihuani'}
8-9
def show_magicians(names):
"""moshushi"""
for name in names:
print(name)
magicians = ['刘谦','大卫.科波菲尔', '大卫.布莱恩','约克']
show_magicians(magicians)
刘谦
大卫.科波菲尔
大卫.布莱恩
约克
8-10
def show_great_magicians(names):
"""moshushi"""
for name in names:
msg = "the Great magician: " + name
print(msg)
magicians = ['刘谦','大卫.科波菲尔', '大卫.布莱恩','约克']
show_great_magicians(magicians)
the Great magician: 刘谦
the Great magician: 大卫.科波菲尔
the Great magician: 大卫.布莱恩
the Great magician: 约克
8-11
def show_magicians(magicians):
for magician in magicians:
print(magician)
def make_great(magician):
for name in magician:
print("\nthe Great " + name)
magicians = ['刘谦','大卫.科波菲尔', '大卫.布莱恩','约克']
magician = []
show_magicians(magicians)
make_great(magicians)
刘谦
大卫.科波菲尔
大卫.布莱恩
约克
the Great 刘谦
the Great 大卫.科波菲尔
the Great 大卫.布莱恩
the Great 约克
8-12
def make_sanwhich(*toppings):
print("\nMaking asanwhich with following toppings: ")
for topping in toppings:
print("-" + topping)
make_sanwhich('popp')
make_sanwhich('faegufi', 'awfb','wfaggr')
Making asanwhich with following toppings:
-popp
Making asanwhich with following toppings:
-faegufi
-awfb
-wfaggr
8-13
def build_profile(first, last, **use_info):
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key, value in use_info.items():
profile[key] = value
return profile
user_profile = build_profile('he', 'yuan wen',
age=23,
hight=175)
print(user_profile)
{'first_name': 'he', 'last_name': 'yuan wen', 'age': 23, 'hight': 175}
8-14
def build_profile(first, last, **use_info):
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key, value in use_info.items():
profile[key] = value
return profile
user_profile = build_profile('subaru', 'outback',
color='green',
tow_package=True)
print(user_profile)
{'first_name': 'subaru', 'last_name': 'outback', 'color': 'green', 'tow_package': True}