个人学习自用,有需者随意取
# 8-1 消息
def display_message():
print("在本章,我学到了如何建立一个函数")
display_message()
# 8-2 喜欢的图书
def favorite_book(title):
print("one of my favorite books is " + title.title() + " . ")
favorite_book('python:from fishman to old driver')
# 8-3 T恤
def make_shirt(size , print_word):
print("\nThis T-shirt's size is " + size + " .")
print("This T-shirt's word is " + print_word + " .")
make_shirt('28' , 'hello world')
# 8-4 大号T恤
def make_shirt(print_word , size='xxx'):
print("\nThis T-shirt's size is " + size + " .")
print("This T-shirt's word is " + print_word + " .")
make_shirt('hello world')
make_shirt('lalala' , 'xx')
# 8-5 城市
def describe_city(name , contry='China'):
print(name.title() + " is in " + contry.title())
describe_city('shanghai')
describe_city('beijing')
describe_city('hangzhou')
# 8-6 城市名
def city_contry(city_name , contry_name):
full = city_name + " , " + contry_name
print(full)
city_contry('Santiago' , 'Chile')
# 8-7 专辑
def make_album(singer_name , album_name , sing_amounts=''):
full_info = {'singer':singer_name , 'album':album_name}
if sing_amounts:
full_info['amounts'] = sing_amounts
print(full_info)
else:
print(full_info)
make_album('me' , '666')
make_album('meme' , '888' , '666')
# 8-8 用户的专辑
while True:
print("\nPlease tell me singer's name:")
print("(enter 'q' at any time to quit)")
singer = input("Singer: ")
if singer == 'q':
break
print("Please tell me album's name:")
album = input("Album: ")
if album == 'q':
break
maked_album = make_album(singer , album)
print(maked_album)
# 8-9 魔术师
def show_magicians(names):
for name in names:
print("\nMagician's name: " + name.title())
magicians_name = ['xie' , 'yi' , 'wen']
show_magicians(magicians_name)
# 8-10 了不起的魔术师
def make_great(past_names , current_name='the Great'):
for past_name in past_names:
full_name = current_name + " " + past_name
print(full_name)
make_great(magicians_name)
# 8-11 不变的魔术师
def make_great(past_names , full_names):
while past_names:
current_names = "the Great " + past_names.pop()
full_names.append(current_names)
magicians_name = ['xie' , 'yi' , 'wen']
full_names = []
make_great(magicians_name[:],full_names)
print(magicians_name)
print(full_names)
# 8-12 三明治
def make_sandwiches(*food):
print(food)
make_sandwiches('cream' , 'bead' , 'jam')
# 8-13 用户简介
def build_profile(first , last , **info):
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key , value in info.items():
profile[key] = value
return profile
user_profile = build_profile('xie' , 'yiwen' ,
location = 'China',
field = 'Japanease',
hobby = 'money'
)
print(user_profile)
# 8-14 汽车
def make_car(maker , type , **other_info):
car_info = {}
car_info['car_maker'] = maker
car_info['car_type'] = type
for key , value in other_info.items():
car_info[key] = value
return car_info
car = make_car('subaru' , 'outback' , color = 'blue' , tow_package = True)
print(car)