8-3 T恤
题目描述:编写一个名为make_shirt()的函数,它接受一个尺码以及要印到T恤上的字样。这个
函数应打印一个句子,概要地说明T恤的尺码和字样。
使用位置实参调用这个函数来制作意=一件T恤;再使用关键字实参来调用这个函数。
代码展示
def make_shirt(size, word):
print("The T-shirt's size is " + size + ", and the word " + word + "is on it")
make_shirt('Large', 'AAA')
make_shirt(size = 'Small', word = 'BBB')
8-4 大号T恤
题目描述:修改函数make_shirt(),使其在默认情况下制作一件印有'I love Python'的大号
T恤。调用这个函数来制作如下T恤:一件印有默认字样的大号T恤、一件印有默认字样的中号T恤和
一件印有其他字样的T恤。
代码展示
def make_shirt(size = 'large', word = 'I love Python'):
print("The T-shirt's size is " + size + ", and the word " + word + "is on it")
make_shirt()
make_shirt('medium')
make_shirt('large', 'Other word')
8-12 三明治
题目描述:编写一个函数,它接受顾客要在三明治中添加的一系列食材。这个函数只有一个形参
,并打印一条消息,对顾客点的三明治进行概述。调用这个函数三次,每次都提供不同数量的实参
代码展示
def make_sandwich(*ingredients):
print('Your sandwich has those ingredients: ')
for ingredient in ingredients:
print(ingredient)
make_sandwich('tomato', 'Bacon')
make_sandwich('potato')
make_sandwich('apple','orange','pear')
8-13 用户简介
题目描述:复制前面的程序user_profile.py,在其中调用build_profile()来创建有关你的
简介;调用这个函数时,指定你的名和姓,以及三个描述你的键-值对。
代码展示
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',
field='physics')
print(user_profile)
my_profile = build_profile('Remilia', 'Scarlet',
location = 'Koumakan',
field = 'destiny',
drink = 'tea'
)
print(my_profile)