第八章课后练习题

时间:2023-02-23 15:11:10

8-1 消息 :编写一个名为display_message() 的函数,它打印一个句子,指出你在本章学的是什么。调用这个函数,确认显示的消息正确无误。

代码:

def display_message():
	print("In this chapter, I have learnt how to write a function.")

display_message()

结果:

In this chapter, I have learnt how to write a function.

8-2 喜欢的图书 :编写一个名为favorite_book() 的函数,其中包含一个名为title 的形参。这个函数打印一条消息,One of my favorite books is Alicein Wonderland 。调用这个函数,并将一本图书的名称作为实参传递给它。 

代码:
def favorite_book(title):
	print("One of my favorite books is "+ title +".")
	

favorite_book("Alicein Wonderland")

结果:

One of my favorite books is Alicein Wonderland.

8-3 T恤 :编写一个名为make_shirt() 的函数,它接受一个尺码以及要印到T恤上的字样。这个函数应打印一个句子,概要地说明T恤的尺码和字样。使用位置实参调用这个函数来制作一件T恤;再使用关键字实参来调用这个函数。

代码:

def make_shirt(size,word):
	print("\nThe shirt is "+ size +".")
	print("The words on the shirt are: "+ word + ".")

make_shirt("L","I love Python")

make_shirt(word="I love Python",size="L")

结果:

The shirt is L.
The words on the shirt are: I love Python.

The shirt is M.
The words on the shirt are: I love Python.

The shirt is L.
The words on the shirt are: Don't give up.

8-4 大号T恤 :修改函数make_shirt() ,使其在默认情况下制作一件印有字样“I love Python”的大号T恤。调用这个函数来制作如下T恤:一件印有默认字样的大号T恤、一件印有默认字样的中号T恤和一件印有其他字样的T恤(尺码无关紧要)。

代码:

def make_shirt(size,word="I love Python"):
	print("\nThe shirt is "+ size +".")
	print("The words on the shirt are: "+ word + ".")

make_shirt("L")
make_shirt("M")
make_shirt("L","Don't give up")

结果:

The shirt is L.
The words on the shirt are: I love Python.

The shirt is M.
The words on the shirt are: I love Python.

The shirt is L.
The words on the shirt are: Don't give up.

8-5 城市 :编写一个名为describe_city() 的函数,它接受一座城市的名字以及该城市所属的国家。这个函数应打印一个简单的句子,如Reykjavik is inIceland 。给用于存储国家的形参指定默认值。为三座不同的城市调用这个函数,且其中至少有一座城市不属于默认国家。 

代码:
def describe_city(city="Beijing",country="China"):
	print(city + " is in " + country + ".")
	
describe_city()
describe_city('London', "England")
describe_city("New York", "America")

结果:

Beijing is in China.
London is in England.
New York is in America.

8-6 城市名 :编写一个名为city_country() 的函数,它接受城市的名称及其所属的国家。这个函数应返回一个格式类似于下面这样的字符串: 

代码:
def city_country(city,country):
	print('"'+city + ", " + country + '"')
	
city_country("Santiago", "Chile")

结果:

"Santiago, Chile"

8-7 专辑 :编写一个名为make_album() 的函数,它创建一个描述音乐专辑的字典。这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典。使用这个函数创建三个表示不同专辑的字典,并打印每个返回的值,以核实字典正确地存储了专辑的信息。

给函数make_album() 添加一个可选形参,以便能够存储专辑包含的歌曲数。如果调用这个函数时指定了歌曲数,就将这个值添加到表示专辑的字典中。调用这个函数,并至少在一次调用中指定专辑包含的歌曲数。

代码:

def make_album(singer,name,number=''):
	album = {'singer':singer,'name': name}
	if number:
		album['number']=number
	return album

album_ = make_album("Yu Wenwen","Timian",1)
print(album_)
album_ = make_album("Jay Chou","Jay")
print(album_)
album_ = make_album("Eason Chan","C'mon in~",10)
print(album_)

结果:

{'singer': 'Yu Wenwen', 'name': 'Timian', 'number': 1}
{'singer': 'Jay Chou', 'name': 'Jay'}
{'singer': 'Eason Chan', 'name': "C'mon in~", 'number': 10}

8-8 用户的专辑 :在为完成练习8-7编写的程序中,编写一个while 循环,让用户输入一个专辑的歌手和名称。获取这些信息后,使用它们来调用函数make_album() ,并将创建的字典打印出来。在这个while 循环中,务必要提供退出途径。 

代码:

def make_album(singer,name,number):
	album = {'singer':singer,'name': name,'number':number}
	return album

album_ = make_album("Yu Wenwen","Timian",1)
album_ = make_album("Jay Chou","Jay" ,10)
album_ = make_album("Eason Chan","C'mon in~",10)

	
while True:
	print("\nPlease input the information:") 
	print("(enter 'q' at any time to quit)")
	singer = input("Singer name: ") 
	if singer == 'q':
		break
	name = input("Album name: ") 
	if name == 'q':
		break
	number = input("Number: ") 
	album_ = make_album(singer,name,number)
	print(album_)

结果:

Please input the information:
(enter 'q' at any time to quit)
Singer name: Yu Wenwen
Album name: Timian
Number: 1
{'singer': 'Yu Wenwen', 'name': 'Timian', 'number': '1'}

Please input the information:
(enter 'q' at any time to quit)
Singer name: Jay Chou 
Album name: Jay
Number: 10
{'singer': 'Jay Chou', 'name': 'Jay', 'number': '10'}

Please input the information:
(enter 'q' at any time to quit)
Singer name: Eason Chan
Album name: C'mon in~
Number: 10
{'singer': 'Eason Chan', 'name': "C'mon in~", 'number': '10'}

Please input the information:
(enter 'q' at any time to quit)
Singer name: q

8-9 魔术师 :创建一个包含魔术师名字的列表,并将其传递给一个名为show_magicians() 的函数,这个函数打印列表中每个魔术师的名字。

代码:

def show_magicians(magicians): 
	for magician in magicians:
		msg = magician.title()
		print(msg)
magicians = ['Tom', 'Jack', 'Amy'] 
show_magicians(magicians)

结果:

Tom
Jack
Amy

8-10 了不起的魔术师 :在你为完成练习8-9而编写的程序中,编写一个名为make_great() 的函数,对魔术师列表进行修改,在每个魔术师的名字中都加入字样“the Great”。调用函数show_magicians() ,确认魔术师列表确实变了。

代码:

def show_magicians(magicians): 
	for magician in magicians:
		msg = magician.title()
		print(msg)
def make_great(magicians):
	while magicians:
		current_="the Great "+magicians.pop()
		current.append(current_)
	    
magicians = ['Tom', 'Jack', 'Amy'] 
current = []
make_great(magicians)
show_magicians(current)

结果:

The Great Amy
The Great Jack
The Great Tom

8-11 不变的魔术师 :修改你为完成练习8-10而编写的程序,在调用函数make_great() 时,向它传递魔术师列表的副本。由于不想修改原始列表,请返回修改后的列表,并将其存储到另一个列表中。分别使用这两个列表来调用show_magicians() ,确认一个列表包含的是原来的魔术师名字,而另一个列表包含的是添加了字样“the Great”的魔术师名字。 

代码:

def show_magicians(magicians): 
	for magician in magicians:
		msg = magician.title()
		print(msg)
def make_great(magicians,current):
	while magicians[:]:
		current_="the Great "+magicians.pop()
		current.append(current_)
	    
magicians = ['Tom', 'Jack', 'Amy'] 
current = []
make_great(magicians[:],current)
show_magicians(current)
show_magicians(magicians)

结果:

The Great Amy
The Great Jack
The Great Tom
Tom
Jack
Amy

8-12 三明治 :编写一个函数,它接受顾客要在三明治中添加的一系列食材。这个函数只有一个形参(它收集函数调用中提供的所有食材),并打印一条消息,对顾客点的三明治进行概述。调用这个函数三次,每次都提供不同数量的实参。

代码:

def make_sandwich(*toppings):
	print(toppings)
make_sandwich('pepperoni')
make_sandwich('mushrooms', 'green peppers', 'extra cheese')
make_sandwich('pork','carrot')

结果:

('pepperoni',)
('mushrooms', 'green peppers', 'extra cheese')
('pork', 'carrot')