#8-7 专辑defmake_album(name,album_name,number = ''):if number:
album = {'name':name,'album_name':album_name,'number':number}
else:
album = {'name':name,'album_name':album_name}
return album
print(make_album('周杰伦','听妈妈的话'))
print(make_album('某某','某某某','5'))
#8-8 用户的专辑defmake_album(name,album_name,number=''):
album = {'name':name,'album_name':album_name}
return album
whileTrue:
print("(enter 'q' at any time to quit)")
name = input('请输入歌手名:')
if name == 'q':
break
album_name = input('请输入专辑名:')
if album_name == 'q':
break
print(make_album(name,album_name))
#8-9 魔术师
magicians = ['ergou','qiqi','shitou']
defshow_magicianslis(magicians):for name in magicians:
print(name)
show_magicianslis(magicians)
#8-10 了不起的魔术师
magicians = ['ergou','qiqi','shitou']
great_magicians = []
defmake_great(magicians,great_magicians):while magicians:
magician = magicians.pop()
great_magician = 'the Great ' + magician
great_magicians.append(great_magician)
defshow_magicianslis(great_magicians):for name in great_magicians:
print(name)
make_great(magicians,great_magicians)
show_magicianslis(great_magicians)
#8-11 不变的魔术师
magicians = ['ergou','qiqi','shitou']
great_magicians = []
defmake_great(magicians,great_magicians):while magicians:
magician = magicians.pop()
great_magician = 'the Great ' + magician
great_magicians.append(great_magician)
defshow_magicianslis(great_magicians):for name in great_magicians:
print(name)
make_great(magicians[:],great_magicians)
show_magicianslis(great_magicians)
show_magicianslis(magicians)
#8-12 三明治defsandwich(*toppings):
print("\nMaking a sandwich with the following toppings:")
for topping in toppings:
print("- " + topping)
sandwich('apple')
sandwich('banana','apple')
#8-13 用户简介defbuild_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('albert','einstein',location='princeton',field='physics',)
print(user_profile)
#8-14 汽车defmake_car(manufacturer, model, **type_info):"""创建一个字典,其中包含我们知道的有关用户的一切"""
profile = {}
profile['manufacturer'] = manufacturer
profile['model'] = model
for key, value in type_info.items():
profile[key] = value
return profile
car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)
#8-15 打印模型#printing_functions.pydefprint_models(unprinted_designs, completed_models):"""
模拟打印每个设计,直到没有未打印的设计为止
打印每个设计后,都将其移到列表completed_models中
"""while unprinted_designs:
current_design = unprinted_designs.pop()
# 模拟根据设计制作3D打印模型的过程
print("Printing model: " + current_design)
completed_models.append(current_design)
defshow_completed_models(completed_models):"""显示打印好的所有模型"""
print("\nThe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
----------------------------------------------
#print_models.pyfrom printing_functions import *
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)
#8-16 导入#displaying.pydefdisplay_message():
print('本章学习的函数')
----------------------------------------------
import displaying
displaying.display_message()
from displaying import display_message
display_message()
from displaying import display_message as dm
dm()
import displaying as dm
dm.display_message()
from displaying import *
display_message()