8-1 消息 : 编写一个名为display_message() 的函数, 它打印一个句子, 指出你在本章学的是什么。 调用这个函数, 确认显示的消息正确无误。
def display_message():
print("In this chapter, we will learn function of Python.")
display_message()
结果:
In this chapter, we will learn function of Python.
8-2 喜欢的图书 : 编写一个名为favorite_book() 的函数, 其中包含一个名为title 的形参。 这个函数打印一条消息, 如One of my favorite books is Alice in Wonderland 。 调用这个函数, 并将一本图书的名称作为实参传递给它。
def favorite_book(title):
print("One of my favourite books is " + title + '.')
favorite_book("Alice in Wonderland")
结果:
One of my favourite books is Alice in Wonderland.
8-3 T
恤 : 编写一个名为
make_shirt()
的函数, 它接受一个尺码以及要印到
T
恤上的字样。 这个函数应打印一个句子, 概要地说明
T
恤的尺码和字样。使用位置实参调用这个函数来制作一件 T 恤; 再使用关键字实参来调用这个函数。
def make_shirt(size, letter):
print("\nHere is the information of your T-shirt.")
print("Size: " + size)
print("Letter: " + letter)
make_shirt('M', "I'm Chinese")
make_shirt(letter = "I'm Chinese", size = 'M')
结果:
Here is the information of your T-shirt.
Size: M
Letter: I'm Chinese
Here is the information of your T-shirt.
Size: M
Letter: I'm Chinese
8-4 大号T恤 : 修改函数make_shirt() , 使其在默认情况下制作一件印有字样“I love Python”的大号T恤。 调用这个函数来制作如下T恤:
一件印有默认字样的大号T恤、一件印有默认字样的中号T恤和一件印有其他字样的T恤(尺码无关紧要) 。
def make_shirt(size='L', letter='I love Python'):
print("\nHere is the information of your T-shirt.")
print("Size: " + size)
print("Letter: " + letter)
m