Python编程:从入门到实践第八章练习8.5
快要做完8-12的时候,镜片突然就从眼睛上掉下来,手一摸,镜框都裂开了,我人也裂开了;
原来,打代码不仅费眼睛,还费眼镜…哎
#coding:gbk
#8-12三明治
def making_sandwiches(*addings):
"""打印顾客点的所有食材"""
print("\nMaking sanwiches with following food: ")
for adding in addings:
print(adding)
making_sandwiches('pepperroni','egg','sausage')
making_sandwiches('extra cheese','peppers','mushrooms')
making_sandwiches('pepperoni','bacon','beaf')
#8-13用户简介
def build_profile(first,last,**user_info): ##形参**user_info中的两个型号让python创建一个名为user_info的空字典,并将收到的所有名称-值对都封装到这个字典中。
"""创建一个字典,其中包含我们所知道的有关用户的一切"""
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key,value in user_info.items(): #遍历字典user_info中的键-值对,并将每个键-值对都加入到字典profile中
profile[key] = value
return profile #最后,将字典profile返回给函数调用行
user_profile = build_profile('john','su',
location = 'Amoy',
field = 'economy',
living = 'fuzhou')
print(user_profile)
#8-14汽车
def making_cars(brand,size,**cars_info):
"""打印汽车信息"""
cars = {}
cars['brand'] = brand
cars['size'] = size
for key,value in cars_info.items():
cars[key] = value
return cars
car_info = making_cars('audi','large',made_in = 'china' , color = 'blue')
print(car_info)