#8-12三文治:编写一个函数,它接受顾客要三文治中添加的一系列食材,这个函数只有一个形参,
# (它收集函数调用中提供的所有食材)并打印一条消息,对顾客点的三文治进行概述,调用这个函数三次,
#每次提供不同数量的实参
def sandwich(*meterials): #*不能省略
"""打印顾客点的所有食材"""
print("This sandwich has:")
for meterial in meterials:
print("-" + meterial)
sandwich('pork','pine','beef')
sandwich('beef','mike')
sandwich('mike')
"""8-13用户简介,复制前面的程序user_peofile.py,在其中调用build_profile()来创建有关你的简介,
调用这个函数,制定你的名和姓,以及三个描述你的键-值对"""
#build_profile.py
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('albert','einstein',location='princeton',file='physics')#两个键-值对
print(user_profile)
user_profile=build_profile('congcong','meng',location='guangzhou',country='china',favoriate='swimming')
print(user_profile)
"""使用任意数量的关键字实参"""
"""8-14汽车:编写一个函数,将一辆汽车的信息存储在一个字典中"""
def make_car(maker,type,**car):
my_car = {}
my_car['maker'] = maker
my_car['type'] =type
for key,value in car.items():
my_car[key] = value
return my_car
car = make_car('subaru','outback',color='blue',tow_package=True)
print(car)
"""任意数量的关键字实参"""
#8-15print_models
def print_models(unprinted_designs,completed_models):
while unprinted_designs:
current_design = unprinted_designs.pop()
print("Printing model:" + current_design)
completed_models.append(current_design)
def show_completed_models(completed_models):
print("\nThe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
unprinted_designs = ['iphone case','robot pendant','dodecahedron']
completed_models = []
print_models(unprinted_designs,completed_models)
show_completed_models(completed_models)