题目:
8-12 三明治 :编写一个函数,它接受顾客要在三明治中添加的一系列食材。这个函数只有一个形参(它收集函数调用中提供的所有食材),并打印一条消息,对顾客
点的三明治进行概述。调用这个函数三次,每次都提供不同数量的实参。
8-13 用户简介 :复制前面的程序user_profile.py,在其中调用build_profile() 来创建有关你的简介;调用这个函数时,指定你的名和姓,以及三个描述你的键-值
对。
8-14 摩托车 :编写一个函数,将一辆汽车的信息存储在一个字典中。这个函数总是接受制造商和型号,还接受任意数量的关键字实参。这样调用这个函数:提供必不可
少的信息,以及两个名称—值对,如颜色和选装配件。这个函数必须能够像下面这样进行调用:
car = make_car('subaru', 'outback', color='blue', tow_package=True)
打印返回的字典,确认正确地处理了所有的信息。
代码:
#!usr/bin/python
# _*_ coding:utf-8 _*_
#三明治
def make_sandwich(*toppings):
"""接收顾客所点三明治的配料。并且进行描述"""
print("你的披萨需要加入下面的配料")
for topping in toppings:
print("--" + topping)
make_sandwich("mushroom","beef")
#用户简介1
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("老",
"王",
field="量子物理",
addre="北京大学")
print(user_profile)
#用户简介2
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("尹",
"成承",
field="计算机网络技术",
addre="家里蹲大学")
print(user_profile)
#制造摩托车
def make_motocycle(manufacturers,modle,**bike_infos):
"""接收参数,描述汽车信息"""
super_bike = {}
super_bike["制造商"] = manufacturers
super_bike["型号"] = modle
for key,value in bike_infos.items():
super_bike[key] = value
return super_bike
super_bike = make_motocycle("BMW","s1000rr",ABS="True",displacement="1000cc")
print(super_bike)