8-1消息
编写一个名为display_message()的函数,它打印一个句子,指出你在本章学的是什么。调用这个函数,确认显示的消息正确无误。
def display_message():
print("I have learned how to define a function and how to deliver parameters to function.")
display_message()
build
I have learned how to define a function and how to deliver parameters to function.
[Finished in 0.2s]
8-2 喜欢的图书
编写一个名为favorite_book()的函数,其中包含一个名为title的形参。这个函数打印一条消息,如One of my favorite books is Alice in Wonderland。调用这个函数,并将一本图书的名称作为实参传递给它。
def favorite_book(title):
print("One of my favorite books is " + title.title() + ".")
favorite_book('alice in wonderland')
#实参的引号不能忘
build
One of my favorite books is Alice In Wonderland.
[Finished in 0.1s]
习题8-7 专辑
(1)
def make_album(album_name, singer_name):
album = {'album': album_name, 'singer': singer_name}
return album
album_information = make_album('Innocence', 'Avaril')
print(album_information)
album_information = make_album('Fantasy', 'Jay')
print(album_information)
album_information = make_album('Baby', 'Biber')
print(album_information)
Build:
{'album': 'Innocence', 'singer': 'Avaril'}
{'album': 'Fantasy', 'singer': 'Jay'}
{'album': 'Baby', 'singer': 'Biber'}
[Finished in 0.2s]
(2)给函数make_album()添加一个可选形参,以便能够存储专辑包含的歌曲数。如果调用这个函数时指定了歌曲数,就将这个值添加到表示专辑的字典中。调用这个函数,并至少在一次调用中指定专辑包含的歌曲数。
def make_album(album_name, singer_name, album_number = "10"):
album = {'album': album_name, 'singer': singer_name, 'number': album_number}
return album
album_information = make_album('Innocence', 'Avaril', 1)
print(album_information)
album_information = make_album('Fantasy', 'Jay')
print(album_information)
Build:
{'album': 'Innocence', 'singer': 'Avaril', 'number': 1}
{'album': 'Fantasy', 'singer': 'Jay', 'number': '10'}
[Finished in 0.2s]
习题8-8 用户的专辑
在为完成练习8-7编写的程序中,编写一个while循环,让用户输入一个专辑的歌手和名称。获取这些信息后,使用它们来调用函数make_album(),并将创建的字典打印出来。在这个while循环中,务必要提供退出途径。
def make_album(album_name, singer_name, album_number = "10"):
album = {'album': album_name, 'singer': singer_name, 'number': album_number}
return album
while True:
print('\nPlease input album_s name and singer_s name:')
print("(enter 'q' at any time to quit)")
a_name = input("Album_name: ")
if a_name == 'q':
break
s_name = input("Singer_name: ")
if s_name == 'q':
break
album_information = make_album(a_name, s_name, "5")
print("\nThe album's information is as follows:")
print(album_information)
Tools----SublimeREPL----Python----RUN current files
Please input album_s name and singer_s name:
(enter 'q' at any time to quit)
Album_name: Innocence
Singer_name: Avaril
The album's information is as follows:
{'album': 'Innocence', 'singer': 'Avaril', 'number': '5'}
Please input album_s name and singer_s name:
(enter 'q' at any time to quit)
Album_name: q
***Repl Closed***
运行结果如上。
习题8-9 魔术师:
创建一个包含魔术师名字的列表,并将其传递给一个名为show_magicians()的函数,这个函数打印列表中每个魔术师的名字。
def show_magicians(magician_names):
for magician_name in magician_names:
msg = magician_name.title()
print(msg)
magician_list = ['David', 'Nike', 'Mlxg']
show_magicians(magician_list)
输出结果
David
Nike
Mlxg
[Finished in 0.2s]
8-10 了不起的魔术师:
在你为完成练习8-9而编写的程序中,编写一个名为make_great()的函数,对魔术师列表进行修改,在每个魔术师的名字中加入字样“the Great”。调用函数show_magicians(),确认魔术师列表确实变了。
magician_list = ['Uzi', 'Xiaohu', 'Mlxg'] #创建一个列表,这里皮了一下嘿嘿嘿
magician_list_great = [] #一个空列表,用来放置修改后的列表元素
def show_magicians(magician_names): #形参magician_names
for magician_name in magician_names: #遍历这个列表中每一个元素
msg = magician_name.title()
print(msg)
#函数1:打印对应列表元素的函数
def make_great(magician_names, magician_names_great):
#与函数1相同的形参1magician_names(为原列表设计的参数),
#形参2 magician_names_great(为空列表设计的参数)
while magician_names:
magician_names_great = "The Great " + magician_names.pop()
print(magician_names_great)
#函数2:修改列表 具体做法是通过弹出列表1中的元素,将其添加到列表2中
#虽然如此,但是其实不符合题目要求,没有达到修改原列表的目的
#但例题p126-127中,也同样创建了两个列表
make_great(magician_list, magician_list_great)
show_magicians(magician_list)
输出结果:
The Great Mlxg
The Great Xiaohu
The Great Uzi
[Finished in 0.1s]
#magician_list已经变成空列表了~
8-11 不变的魔术师:
修改你为完成练习8-10而编写的程序,在调用函数make_great()时,向它传递魔术师列表的副本。由于不想修改原始列表,请返回修改后的原列表,并将其存储到另一个列表中。分别使用这两个列表来调用show_magicians(),确认一个列表包含的是原来的魔术师名字,而另一个列表包含的是添加了字样“the Great”的魔术师的名字。
magician_list = ['Uzi', 'Xiaohu', 'Mlxg']
magician_list_great = []
def show_magicians(magician_names):
for magician_name in magician_names:
msg = magician_name.title()
print(msg)
def make_great(magician_names, magician_names_great):
while magician_names:
magician_names_great = "The Great " + magician_names.pop()
print(magician_names_great)
make_great(magician_list[:], magician_list_great) #切片 创建列表的副本
show_magicians(magician_list)
输出结果:
The Great Mlxg
The Great Xiaohu
The Great Uzi
Uzi
Xiaohu
Mlxg
[Finished in 0.1s]