8-1 消息
def display_message () :
""""指出在本章学习的是什么。"""
print("本章学习的是函数" )
display_message()
8-2 喜欢的图书
def favorite_book (title) :
""""介绍喜欢的图书"""
print("One of my favorite books is " + title + "." )
favorite_book('Alice in Wonderland' )
8-3 T恤
def make_shirt (size,write_what) :
"""说明T恤的尺码和字样。"""
print("T恤的尺码是 " + size + ",上面写着" + write_what + "。" )
make_shirt(size = 'large' ,write_what = 'Nothing is impossible.' )
8-4 大号T恤
def make_shirt (size = 'large' ,write_what = 'I love Python.' ) :
"""说明T恤的尺码和字样。"""
print("T恤的尺码是 " + size + ",上面写着" + write_what + "。" )
make_shirt()
make_shirt('medium' )
make_shirt(write_what = 'Nothing is impossible.' )
8-5 城市
def describe_city (city_name = 'Wuhan' ,country = 'China' ) :
"""这座城市的名字以及该城市所属的国家"""
print(city_name + "is in " + country + "." )
describe_city()
describe_city('Beijing' )
describe_city('New York' ,'America' )
8-6 城市名
def city_country (city_name,country) :
"""这座城市的名字以及该城市所属的国家"""
message = city_name + ", " + country
return message.title()
city1 = city_country('santiago' ,'chile' )
city2 = city_country('wuhan' ,'china' )
city3 = city_country('new york' ,'america' )
print(city1)
print(city2)
print(city3)
8-7 专辑
def make_ablum (singer_name,ablum_name,songs_number = '' ) :
"""专辑的名字以及歌手的名字"""
message = {'singer' :singer_name,'ablum' :ablum_name,'number' :songs_number}
return message
ablum1 = make_ablum('Jane Zhang' ,'Change' )
ablum2 = make_ablum('Lady Gaga' ,'Artpop' ,15 )
ablum3 = make_ablum('Kesha' ,'Rainbow' ,14 )
print(ablum1)
print(ablum2)
print(ablum3)
8-8 用户的专辑
def make_ablum (singer_name,ablum_name) :
"""用户的专辑以及歌手"""
message = {'singer' :singer_name,'ablum' :ablum_name}
return message
while True :
print("\n请输入歌手名字:" )
print("请输入专辑名称:" )
print("(enter 'q' at any time to quit.)" )
s_name = input("歌手名字:" )
s_name = s_name.title()
if s_name == 'Q' :
break
a_name = input("专辑名称:" )
a_name = a_name.title()
if a_name == 'Q' :
break
ablum = make_ablum(s_name,a_name)
print(ablum)
8-9 魔术师
def show_magicians (names) :
"""打印列表中每个魔术师的名字。"""
for name in names:
if __name__ == '__main__' :
message = "Hello, " + name.title() + "!"
print(message)
lists = ['dynamo' ,'Jason Latimer' ,'Louis Liu' ]
show_magicians(lists)
8-10 了不起的魔术师
def make_great (magician_name) :
"""对魔术师列表进行修改。"""
for i in range(3 ):
magician_name[i] = 'the Great ' + magician_name[i]
return magician_name
def show_magicians (names) :
"""打印列表中每个魔术师的名字。"""
for name in names:
print("Hello " + name.title() + "!" )
lists = ['dynamo' ,'Jason Latimer' ,'Louis Liu' ]
make_great(lists)
show_magicians(lists)
8-11 不变的魔术师
def make_great (magician_name) :
"""对魔术师列表进行修改。"""
for i in range(3 ):
magician_name[i] = 'the Great ' + magician_name[i]
return magician_name
def show_magicians (names) :
"""打印列表中每个魔术师的名字。"""
for name in names:
print("Hello " + name.title() + "!" )
lists = ['dynamo' ,'Jason Latimer' ,'Louis Liu' ]
great_magician = make_great(lists[:])
show_magicians(lists)
show_magicians(great_magician)
8-12 三明治
def make_sandwich (*toppings) :
"""概述要制作的三明治"""
print("\n制作三明治需要以下食材:" )
for topping in toppings:
print("-" + topping)
make_sandwich('ham' )
make_sandwich('mushroom' ,'tomato' )
make_sandwich('egg' ,'vegetable' ,'cheese' )
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('Rosalyn' ,'Yu' ,location = 'Hubei' ,field = 'Electronic Information' ,hobby = 'sing' )
print(user_profile)
8-14 汽车
def make_car (manufacturer,model,**user_info) :
"""将一辆汽车的信息存储在一个字典中"""
profile = {}
profile['manufacturer' ] = manufacturer
profile['model' ] = model
for key,value in user_info.items():
profile[key] = value
return profile
car = make_car('subaru' ,'outback' ,color = 'blue' ,tow_package = True )
print(car)
8-15 打印模型
import printing_functions as pf
unprinted_designs = ['iphone case' ,'robot pendant' ,'dodecahedron' ]
completed_models = []
pf.print_models(unprinted_designs,completed_models)
pf.show_completed_models(completed_models)
8-16 导入
import printing_functions
from printing_functions import print_models
from printing_functions import print_models as pm
import printing_functions as pm
from printing_functions import *