def display_message():
“”“显示简单的语句”""
print(“We will learn function from this chapter.”)
display_message()
print()
def favorite_book(title): # 注意这里形参不需要加单引号
“”“显示喜欢的图书”""
print("One of my favorite books is " + title + “.”)
favorite_book(‘Alice in Wonderland’) # 注意括号里面的实参要加单引号
def make_shirt(size, word):
“”“显示T恤尺码及字样”""
print("\nThe size is " + size + “.”) # 可以在size前面加str()
print("The word on the T-shirt is " + word + “.”)
make_shirt(‘S’, ‘Hello World’)
def make_shirt(size = ‘L’, word = ‘I love Python’):
“”“显示默认‘我爱Python字样’的大号T恤”""
print("\nThe size is " + size + “.”)
print("The word on the T-shirt is " + word + “.”)
make_shirt()
make_shirt(size = ‘M’)
make_shirt(size = ‘S’, word = ‘Hello world’)
def describe_city(city_name, nation = ‘China’):
“”“城市名字及所属国家”""
print("\n" + city_name + " is in " + nation + “.”)
describe_city(‘Beijing’)
describe_city(‘Shenzhen’)
describe_city(‘London’, ‘England’)
print()
def city_country(city, country):
“”“返回城市及所属国家”""
str = city + ", " + country
return str.title()
number1 = city_country(‘beijing’, ‘china’)
number2 = city_country(‘shenzhen’, ‘china’)
number3 = city_country(‘london’, ‘england’)
print(number1)
print(number2)
print(number3)
print()
def make_album(singer_name, album_name, number=’’):
“”“返回专辑字典”""
album = {‘singer’: singer_name, ‘album’: album_name}
if number:
album[‘number’] = number
return album
album1 = make_album(‘Jay Chou’, ‘Listen to mother’)
album2 = make_album(‘Singer 2’, ‘Album 2’, 2)
album3 = make_album(‘Singer 3’, ‘Album 3’, 5)
print(album1)
print(album2)
print(album3)
def make_album(singer, album, num=’’):
albums = {}
if num:
albums[‘singer’] = singer
albums[‘album’] = album
albums[‘num’] = num
else:
albums[‘singer’] = singer
albums[‘album’] = album
return albums
u87 = make_album(‘陈奕迅’, ‘U87’)
u87_num = make_album(‘陈奕迅’, ‘U87’, num=12)
happy = make_album(‘陈奕迅’, ‘我的快乐时代’, num=15)
print(u87)
print(u87_num)
print(happy)
print()
def make_album(singer_name, album_name, number=’’):
“”“返回专辑字典”""
album = {‘singer’: singer_name, ‘album’: album_name}
if number:
album[‘number’] = number
return album
while True:
print("\nPlease type singer’s name and album’s name:")
print("(enter ‘quit’ any time to quit)")
s_name = input(“Singer name:”)
if s_name == ‘quit’:
break
a_name = input(“Album name:”)
if a_name == ‘quit’:
break
album0 = make_album(s_name, a_name)
print(album0)
print()
unprinted_magicians = [‘earvin johnson’, ‘elymas’, ‘aubrey’, ‘arien’,]
completed_magicians = []
while unprinted_magicians:
current_magician = unprinted_magicians.pop()
print("Printing names: " + current_magician.title())
completed_magicians.append(current_magician)
print(“The following names have been printed:”)
for completed_magician in completed_magicians:
print(completed_magician)
print()
def print_magicians(unprinted_magicians,completed_magicians):
“”"
模拟打印每个名字,直到没有未打印的名字为止
打印每个名字后,都将其移到列表completed_magicians中
“”"
while unprinted_magicians:
current_magician = unprinted_magicians.pop()
print(“Printing names: " + current_magician.title())
completed_magicians.append(current_magician)
def show_magicians(completed_magicians):
“”“显示打印好的所有名字””"
print(“The following names have been printed:”)
for completed_magician in completed_magicians:
print(completed_magician)
unprinted_magicians = [‘earvin johnson’, ‘elymas’, ‘aubrey’, ‘arien’,]
completed_magicians = []
print_magicians(unprinted_magicians,completed_magicians)
show_magicians(completed_magicians)
print()
name_list = [‘mag1’,‘mag2’,‘mag3’]
def show_magicians():
for name in name_list:
print(name)
show_magicians()
def print_great(unprinted_magicians, printed_magicians):
while unprinted_magicians:
current_magician = unprinted_magicians.pop()
print("The great " + current_magician.title())
printed_magicians.append(current_magician)
def show_great(printed_magicians):
for printed_magician in printed_magicians:
print(printed_magician)
unprinted_magicians = [‘criss’, ‘jason’, ‘cyril’]
printed_magicians = []
print_great(unprinted_magicians,printed_magicians)
show_great(printed_magicians)
print()
print(“8-10网上参考答案”)
name_list = [‘mag1’,‘mag2’,‘mag3’]
name_change = []
def make_great(name_list,name_change):
while name_list:
cur = name_list.pop()
cur = 'the great ’ + cur
name_change.append(cur)
def show_magicians(name_change):
for name in name_change:
print(name)
make_great(name_list,name_change)
show_magicians(name_change)
print()
print(“8-11网上参考答案”)
name_list = [‘mag1’,‘mag2’,‘mag3’]
name_change = []
def make_great(name_list,name_change):
while name_list:
cur = name_list.pop()
cur = 'the great ’ + cur
name_change.append(cur)
def show_magicians(name_change):
for name in name_change:
print(name)
make_great(name_list[:],name_change)
show_magicians(name_list)
show_magicians(name_change)
print("\n8-12三明治")
def make_sandwich(*toppings): # 也可以用ingredients
for topping in toppings:
print(“We will add " + topping + " on sandwich.”)
make_sandwich(‘onions’, ‘green pepper’, ‘oliver’,)
make_sandwich(‘pepper’)
make_sandwich(‘chicken’, ‘oliver’)
print("\n8-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(‘tina’, ‘wang’,
location = ‘shenzhen’,
field = ‘finance’,
favorite = ‘reading’,)
print(user_profile)
print("\n8-14汽车")
def build_car(manufacturer, model, **car_info):
car = {}
car[“car’s manufacturer”] = manufacturer
car[“car’s model”] = model
for key, value in car_info.items():
car[key] = value
return car
car_1 = build_car(‘subaru’, ‘outback’,
color = ‘blue’,
tow_package = True,)
print(car_1)
import printing_functions
unprinted_designs = [‘nokia case’, ‘robot pendant’, ‘dodecahedron’]
completed_models = []
printing_functions.print_models(unprinted_designs, completed_models)
printing_functions.show_completed_models(completed_models)
print()
import pizza
pizza.make_pizza(15, ‘onion’, ‘green pepper’)
from pizza import make_pizza
make_pizza(12, ‘corn’)
from pizza import make_pizza as mp
mp(10, ‘apple’)
import pizza as p
p.make_pizza(22, ‘banana’)
from printing_functions import *
unprinted_designs = [‘samsung case’, ‘robot pendant’, ‘dodecahedron’]
completed_models = []
printing_functions.print_models(unprinted_designs, completed_models)
printing_functions.show_completed_models(completed_models)