您的位置:首页 > 编程语言 > Python开发

Python编程:从入门到实践——练习题答案(第八章)

2019-02-27 22:24 961 查看

第8章 函数

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

def display_message():
print("function")

display_message()

运行结果:

function

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

def favorite_book(title):
print("One of my favorite book is " + title)

favorite_book('Alice in Wonderland')

运行结果:

One of my favorite book is Alice in Wonderland

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

def make_shirt(size,message):
print("size: " + size)
print("message: " + message)

#使用位置实参调用
make_shirt('large','Hello World!')
#使用关键字实参调用
make_shirt(size = 'large',message = 'Hello World!')

运行结果:

size: large
message: Hello World!
size: large
message: Hello World!

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

def make_shirt(size = 'large',font = 'I love Python'):
print("size: " + size)
print("font: " + font)

make_shirt()
make_shirt(size = 'middle')
make_shirt(size = 'small',font = 'Hello World')

运行结果:

size: large
font: I love Python
size: middle
font: I love Python
size: small
font: Hello World

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

def describe_city(city, country = 'China'):
print(city + " is in " + country)

describe_city('Beijing')
describe_city('Shanghai')
describe_city('NewYork', 'USA')

运行结果:

Beijing is in China
Shanghai is in China
NewYork is in USA

8-6 城市名:编写一个名为 city_country()的函数,它接受城市的名称及其所属的国家。这个函数应返回一个格式类似于下面这样的字符串:"Santiago, Chile"至少使用三个城市国家对调用这个函数,并打印它返回的值。

def city_country(city,country):
city_country = city.title() + "," + country.title()
return city_country

city_country1 = city_country('beijing','china')
print(city_country1)

city_country2 = city_country('shanghai','china')
print(city_country2)

city_country3 = city_country('dalian','china')
print(city_country3)

运行结果:

Beijing,China
Shanghai,China
Dalian,China

8-7 专辑:编写一个名为 make_album()的函数,它创建一个描述音乐专辑的字典。这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典。使用这个函数创建三个表示不同专辑的字典,并打印每个返回的值,以核实字典正确地存储了专辑的信息。
给函数 make_album()添加一个可选形参,以便能够存储专辑包含的歌曲数。如果调用这个函数时指定了歌曲数,就将这个值添加到表示专辑的字典中。调用这个函数,并至少在一次调用中指定专辑包含的歌曲数。

def make_album(artist,title,num = ''):
artist_album = {'artist' : artist,'album' : title}
if num:
artist_album['number'] = num
return artist_album

album1 = make_album('GEM','paomo')
print(album1)
album2 = make_album('GEM','paomo','100w')
print(album2)

运行结果:

{'artist': 'GEM', 'album': 'paomo'}
{'artist': 'GEM', 'album': 'paomo', 'number': '100w'}

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

def make_album(artist,title,num = ''):
artist_album = {'artist' : artist,'album' : title}
if num:
artist_album['number'] = num
return artist_album

print("Enter 'quit' at any time to stop.")
while True:
title = input("What's your favorite album? ")
if title == 'quit':
break
artist = input("Who's the artist? ")
if artist == 'quit':
break

album = make_album(artist,title)
print(album)

运行结果:

Enter 'quit' at any time to stop.
What's your favorite album? paomo
Who's the artist? GEM
{'artist': 'GEM', 'album': 'paomo'}
What's your favorite album? quit

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

def show_magicians(magicians):
for magician in magicians:
print(magician)

magicians = ['Mike', 'John', 'Bob']
show_magicians(magicians)

运行结果:

Mike
John
Bob

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

def show_magicians(magicians):
for magician in magicians:
print(magician)

def make_great(magicians):

great_magicians = []

while magicians:
magician = magicians.pop()
great_magician = magician + ' the Great'
great_magicians.append(great_magician)

for great_magician in great_magicians:
magicians.append(great_magician)

magicians = ['Mike', 'John', 'Bob']
make_great(magicians)
show_magicians(magicians)

运行结果:

Bob the Great
John the Great
Mike the Great

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

def show_magicians(magicians):
for magician in magicians:
print(magician)

def make_great(magicians):

great_magicians = []

while magicians:
magician = magicians.pop()
great_magician = magician + ' the Great'
great_magicians.append(great_magician)

for great_magician in great_magicians:
magicians.append(great_magician)

return magicians

magicians = ['Mike', 'John', 'Bob']
show_magicians(magicians)

print("\nGreat magicians:")
great_magicians = make_great(magicians[:])
show_magicians(great_magicians)

print("\nOriginal magicians:")
show_magicians(magicians)

运行结果:

Mike
John
Bob
Great magicians:
Bob the Great
John the Great
Mike the Great
Original magicians:
Mike
John
Bob

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

def make_sandwish(*toppings):
print("\nMaking a sandwish with the following topping:")
for topping in toppings:
print(topping)

make_sandwish('cheese')
make_sandwish('beef','cheese')
make_sandwish('beef','cheese','butter')

运行结果:

Making a sandwish with the following topping:
cheese

Making a sandwish with the following topping:
beef
cheese

Making a sandwish with the following topping:
beef
cheese
butter

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('Steven', 'Lee',
country='China',
location='Dalian',
field='IT')
print(user_profile)

运行结果:

{'first_name': 'Steven', 'last_name': 'Lee', 'country': 'China',
'location': 'Dalian', 'field': 'IT'}

8-14 汽车:编写一个函数,将一辆汽车的信息存储在一个字典中。这个函数总是接受制造商和型号,还接受任意数量的关键字实参。这样调用这个函数:提供必不可少的信息,以及两个名称—值对,如颜色和选装配件。这个函数必须能够像下面这样进行调用:car = make_car(‘subaru’, ‘outback’, color=‘blue’, tow_package=True)打印返回的字典,确认正确地处理了所有的信息。

def make_car(maker,model, **car_info):
information = {}
information['car_maker'] = maker
information['car_model'] = model
for key, value in car_info.items():
information[key] = value
return information

car = make_car('subaru', 'outback', color='blue' ,tow_package=True)
print(car)

运行结果:

{'car_maker': 'subaru', 'car_model': 'outback',
'color': 'blue', 'tow_package': True}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: