#8-1 消息
def display_message():
"""指出你在本章学的是什么"""
print("学习定义函数")
display_message()
#8-2 喜欢的图书
def favorite_book(title):
"""喜欢的图书"""
print("\nOne of my favorite books is " + title + ".")
favorite_book('The old man and the sea')
#8-3 T恤
def make_shirt(T_size, T_type):
print("\nThe T-shirt's size is: " + T_size)
print("The T-shirt's type is: " + T_type)
make_shirt('39', 'freestyle')
make_shirt(T_size='39', T_type='freestyle')
#8-4 大号T恤
def make_shirt(T_size, T_type='I love python'):
print("\nThe T-shirt's size is: " + T_size)
print("The T-shirt's type is: " + T_type)
make_shirt('45')
make_shirt('40')
make_shirt('39', 'Beliver')
#8-5 城市
def describe_city(name, country='China'):
print("\n" + name.title() +" is in " + country.title())
describe_city('hangzhou')
describe_city('changsha')
describe_city('new york', 'america')
#8-7 专辑
def make_album(singer, album_name, song_number=''):
"""描述音乐专辑的字典"""
album = {'singer': singer, 'album name': album_name}
if song_number:
album['song number'] = song_number
return album
musician = make_album('jay', 'fantasy')
print(musician)
musician = make_album('jay', 'fantasy', '10')
print(musician)
#8-8 用户的专辑
while True:
print("\nPlease enter the information of album: ")
print("(enter 'quit' any time to quit)")
s = input("singer: ")
if s == 'quit':
break
a_n = input("album name: ")
if a_n == 'quit':
break
musician = make_album(s, a_n)
print(musician)
#8-9 魔术师
def show_magicians(magician_names):
"""显示所有魔术师名字"""
for magician_name in magician_names:
print(magician_name)
magician_names = ['magician_1', 'magician_2', 'magician_3']
show_magicians(magician_names)
#8-10 了不起的魔术师
def make_great(magician_names):
"""修改魔术师列表,加the great"""
n = 0
while n < len(magician_names):
magician_names[n] = "the Great " + magician_names[n]
n += 1
magician_names = ['magician_1', 'magician_2', 'magician_3']
make_great(magician_names)
show_magicians(magician_names)
#8-11 不变的魔术师
def make_great(magician_names):
"""修改魔术师列表,加the great"""
n = 0
while n < len(magician_names):
magician_names[n] = "the Great " + magician_names[n]
n += 1
return magician_names
magician_names = ['magician_1', 'magician_2', 'magician_3']
great_magician_names = make_great(magician_names[:])
show_magicians(magician_names)
show_magicians(great_magician_names)
#8-12 三明治
def make_sandwich(*toppings):
"""添加食材"""
print(toppings)
make_sandwich('mushrooms')
make_sandwich('mushrooms', 'green peppers')
make_sandwich('mushrooms', 'green peppers', 'extra cheese')
#8-13 用户简介
def build_profile(first, last, **user_info):
"""创建一个字典,其中包含我们知道的有关用户的一切"""
profile = {}
profile['first'] = first
profile['last'] = last
for k, v in user_info.items():
profile[k] = v
return profile
user_profile = build_profile('albert', 'einstein',
location='princeton',
field='physics',
IQ='200')
print(user_profile)
#8-14 汽车
def make_car(manufacturer, model, **other):
"""创建一个字典,其中包含汽车的信息"""
car = {}
car['manufacturer'] = manufacturer
car['model'] = model
for k,v in other.items():
car[k] = v
return car
car = make_car('subaru', 'outback', color='bule', tow_package=True)
print(car)
#8-15 打印模型
#8.6.1 导入整个模块
import printing_functions
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
printing_functions.print_models(unprinted_designs, completed_models)
printing_functions.show_completed_models(completed_models)
#8.6.2 导入特定的函数
from printing_functions import print_models, show_completed_models
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)
#8.6.3 使用as给函数指定别名
from printing_functions import print_models as pm, show_completed_models as scm
pm(unprinted_designs, completed_models)
scm(completed_models)
#8.6.4 使用as给模块指定别名
import printing_functions as pf
pf.print_models(unprinted_designs, completed_models)
pf.show_completed_models(completed_models)
#8.6.5 导入模块中的所有函数(不建议使用)
# ~ from printing_functions import *
#8-16 导入(同上)