python编程:从入门到实践 第八章课后题
8-1:消息
编写一个名为display_message() 的函数,它打印一个句子,指出你在本章学的是什么。调用这个函数,确认显示的消息正确无误。
def display_message():
print("I will learn the usage of function in this chapter!")
display_message()
In [1]: I will learn the usage of function in this chapter!
8-3
def make_shirt(size, font):
print("This T-shirt is " + str(size) + " big, the '" + font + "' is on it")
make_shirt(32,"hello")
make_shirt(size = 32, font = "hello")
In [2]: runfile(...)
This T-shirt is 32 big, the 'hello' is on it
This T-shirt is 32 big, the 'hello' is on it
8-6
def city_country(city,country):
return str(city.title() + ', ' + country.title())
cc = [('beijing','China'),('New York','American'),('St.Petersburg','Russia')]
for city,country in cc:
print(city_country(city,country))
In [3]: runfile(...)
Beijing, China
New York, American
St.Petersburg, Russia
8-9
def show_magicians(megicians):
for me in megicians:
print(me)
megicians = ['wuda',"wuer","wusan"]
show_magicians(megicians)
In [4]: runfile(...)
wuda
wuer
wusan
8-13
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('huang', 'dirun',
age=20,
field='cs',
favorite='sports'
)
print(user_profile)
In [5]: runfile(...)
{'first_name': 'huang', 'last_name': 'dirun', 'age': 20, 'field': 'cs', 'favorite': 'sports'}