8-1 消息: 编写一个名为display_message()的函数,它打印一个句子,指出你在本章学的是什么。调用这个函数,确认显示的消息正确无误。
def display_message():
print ("I'm learning function!")
display_message()
结果:
I'm learning function!
8-3 T恤: 编写一个名为make_shirt()的函数,它接受一个尺码以及要印到T恤上的字样。这个函数应打印一个句子,概要地说明T恤的尺码和字样。
使用位置实参调用这个函数来制作一件T恤;再使用关键字实参来调用这个函数。
def make_shirt(size, pattern):
print ("The size of T-shirt is " + str(size)
+ " and it's pattern is " + pattern + ".")
make_shirt(42, 'C++')
make_shirt(pattern='Python', size=43)
结果:
The size of T-shirt is 42 and it's pattern is C++.
The size of T-shirt is 43 and it's pattern is Python.
8-6 城市名: 编写一个名为city_country()的函数,它接受城市的名称及其所属的国家。这个函数应返回一个格式类似于下面这样的字符串:
“Santiago, Chile”
def city_country(city, country):
return city + ", " + country
message = city_country('Sichuan', 'China')
print (message)
message = city_country('London', 'England')
print (message)
message = city_country('New York', 'America')
print (message)
结果:
Sichuan, China
London, England
New York, America