Python编程从入门到实践第八章-函数
8-1
def display():
print("I will learn function in this chapter")#函数 function chapter章节
display()8-2
def favorite_book(title):
print("One of my favorite book is " + title.title() + ".")
favorite_book(‘sword coming‘)8-3
def make_shirt(size,style):
print("The T-shirt‘s size is " + str(size) +",and style is " + style + ".")
make_shirt("22","Letter‘SB‘")8-4
def make_shirt(size,style=‘I love Python‘):
print("The T-shirt‘s size is " + size +",and style is " + style + ".")
make_shirt(‘big‘)
make_shirt(‘middle‘,‘I love c#‘)
输出:
The T-shirt‘s size is big,and style is I love Python.
The T-shirt‘s size is middle,and style is I love c#.8-5
def city_coutry(city,country=‘China‘):
print(city + " is in " + country + ".")
city_coutry(‘beijing‘)
city_coutry(‘hebei‘)
city_coutry(‘pairs‘,‘France‘)
输出:
beijing is in China.
hebei is in China.
pairs is in France.8-6 太垃圾了。。
def city_coutry(city,country):
inp = city + ‘,‘ + country
return inp.title()
citys = city_coutry(‘beijing‘,‘china‘)
print(citys)
citys = city_coutry(‘shanghai‘,‘china‘)
print(citys)
citys = city_coutry(‘shenzhen‘,‘china‘)
print(citys)8-7
def make_album(singer_name,album_name,song_num=‘‘):
if song_num:
makea = {‘singer‘:singer_name,‘album‘:album_name,‘num‘:song_num}
else:
makea = {‘singer‘:singer_name,‘album‘:album_name}
return makea
makeal = make_album(‘wugu‘,‘caoge‘,‘15‘)
print(makeal)
makeal = make_album(‘woyebuxiangzheyang‘,‘liujiaying‘)
print(makeal)8-8
def make_album(singer_name,album_name,song_num=‘‘):
makea = {‘singer‘:singer_name,‘album‘:album_name,‘num‘:song_num}
return makea
while True:
print("\npPlease enter the name of singer and album you like:")
print("enter ‘q‘ to quit!")
singer_name = input("singer_name:")
if singer_name == ‘q‘:
break
album_name = input("album_name:")
if album_name == ‘q‘:
break
输出:
pPlease enter the name of singer and album you like:
enter ‘q‘ to quit!
singer_name:liujiaying
album_name:woyebuxiangzheyang
pPlease enter the name of singer and album you like:
enter ‘q‘ to quit!
singer_name:q8-9
def show_magicans(names):
for name in names:
print(name)
magican_name = (‘ayya‘,‘xixi‘,‘wuwu‘)
show_magicans(magican_name)
输出:
ayya
xixi
wuwu8-10照着书上一步步来的,,不太明白意义
magican_names = [‘ayya‘,‘xixi‘,‘wuwu‘]
great_magicans = []
def make_great(magican_names,great_magicans):
while magican_names:
magican_name = magican_names.pop()
great_magican = ‘the great ‘ + magican_name
great_magicans.append(great_magican)
def show_magicans(great_magicans):
for name in great_magicans:
print(name)
make_great(magican_names,great_magicans)
show_magicans(great_magicans)
输出;
the great wuwu
the great xixi
the great ayya8-11 在8-10 中, 倒数第二行改成make_great(magican_names[:],great_magicans) 验证结果 传给show_magicans() magican_names即可
8-12
def pizza_ins(*toppings):
print(toppings)
pizza_ins(‘pingguo‘,‘putao‘,‘liulian‘)8-13
def build_profile(first,last,**user_info):
profile = {}
profile[‘first_name‘] = first
profile[‘last_name‘] = last
for key,value in user_info.items():
profile[key] = value
return profile
user_profile = build_profile(‘yu‘,‘zhang‘,
age =‘22‘,sex =‘man‘)
print(user_profile)8-14 就改了几个词。。
def make_car(first,last,**user_info):
profile = {}
profile[‘first_name‘] = first
profile[‘last_name‘] = last
for key,value in user_info.items():
profile[key] = value
return profile
car = make_car(‘sabru‘,‘outback‘,
color =‘blue‘,tow_package =‘True‘)
print(car)8-15 略
鸽了3天..