《Python程式設計:從入門到實踐》第8章-函式 習題
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]