第八章作業
8-1 消息 : 編寫一個名為display_message() 的函數, 它打印一個句子, 指出你在本章學的是什么。 調用這個函數, 確認顯示的消息正確無誤。
8-2 喜歡的圖書 : 編寫一個名為favorite_book() 的函數, 其中包含一個名為title 的形參。 這個函數打印一條消息, 如One of my favorite books is
Alice in Wonderland 。 調用這個函數, 並將一本圖書的名稱作為實參傳遞給它。
8-1
def display_message(): '''定義函數''' print('類似於定義一個x輸出y的函數類型') display_message() 輸出;類似於定義一個x輸出y的函數類型
8-2
def favorite_book(title ): print('One of my favorite books is ' + title.title()) favorite_book('alice in wonderland') 輸出: One of my favorite books is Alice In Wonderland
8-3 T恤 : 編寫一個名為make_shirt() 的函數, 它接受一個尺碼以及要印到T恤上的字樣。 這個函數應打印一個句子, 概要地說明T恤的尺碼和字樣。
使用位置實參調用這個函數來制作一件T恤; 再使用關鍵字實參來調用這個函數。
8-4 大號T恤 : 修改函數make_shirt() , 使其在默認情況下制作一件印有字樣“I love Python”的大號T恤。 調用這個函數來制作如下T恤: 一件印有默認字樣的大號T
恤、 一件印有默認字樣的中號T恤和一件印有其他字樣的T恤( 尺碼無關緊要) 。
8-5 城市 : 編寫一個名為describe_city() 的函數, 它接受一座城市的名字以及該城市所屬的國家。 這個函數應打印一個簡單的句子, 如Reykjavik is in
Iceland 。 給用於存儲國家的形參指定默認值。 為三座不同的城市調用這個函數, 且其中至少有一座城市不屬於默認國家。
8-3
def make_shirt(recive_size,shirt_words): '''接受一個尺碼以及要印到T恤上的字樣''' print('我接受一個' + recive_size + '碼') print('要印到T恤上的字樣是' + shirt_words) make_shirt(recive_size='XL',shirt_words='曹富最帥') 輸出: 我接受一個XL碼 要印到T恤上的字樣是曹富最帥
8-4
def make_shirt(recive_size,shirt_words='I love Python'): '''接受一個尺碼以及要印到T恤上的字樣''' print('\n我接受一個' + recive_size + '碼') print('要印到T恤上的字樣是' + shirt_words + '!') make_shirt(recive_size='大號') make_shirt(recive_size='中號') make_shirt(recive_size='小號') 輸出: 我接受一個大號碼 要印到T恤上的字樣是I love Python! 我接受一個中號碼 要印到T恤上的字樣是I love Python! 我接受一個小號碼 要印到T恤上的字樣是I love Python!
8-5
def describe_city(citv_name,city_country='Iceland'):#默認值是等於號 '''接受一座城市的名字以及該城市所屬的國家''' print(citv_name.title() + ' is in ' + city_country.title()) describe_city(citv_name='Reykjavik') describe_city(citv_name='aierlan') describe_city(citv_name='beijing',city_country='china') 輸出: Reykjavik is in Iceland Aierlan is in Iceland Beijing is in China
8-6 城市名 : 編寫一個名為city_country() 的函數, 它接受城市的名稱及其所屬的國家。 這個函數應返回一個格式類似於下面這樣的字符串:
"Santiago, Chile"
答:
def city_country(city,country): message = city.title() + ',' + country.title() return message print(city_country('beijing','china')) 輸出: Beijing,China
8-7 專輯 : 編寫一個名為make_album() 的函數, 它創建一個描述音樂專輯的字典。 這個函數應接受歌手的名字和專輯名, 並返回一個包含這兩項信息的字典。 使
用這個函數創建三個表示不同專輯的字典, 並打印每個返回的值, 以核實字典正確地存儲了專輯的信息。
給函數make_album() 添加一個可選形參, 以便能夠存儲專輯包含的歌曲數。 如果調用這個函數時指定了歌曲數, 就將這個值添加到表示專輯的字典中。 調用這個
函數, 並至少在一次調用中指定專輯包含的歌曲數。
8-8 用戶的專輯 : 在為完成練習8-7編寫的程序中, 編寫一個while 循環, 讓用戶輸入一個專輯的歌手和名稱。 獲取這些信息后, 使用它們來調用函
數make_album() , 並將創建的字典打印出來。 在這個while 循環中, 務必要提供退出途徑。
8-7 要求一
def make_album(songer_name,album_name,total_songs=''): '''創建一個描述音樂專輯的字典''' music = {'songer':songer_name,'album':album_name} return music mussican = make_album('zhoujielun','qilixiang') print(mussican) mussican = make_album('chenyixun','meigui') print(mussican) mussican = make_album('naying','heiye') print(mussican) 輸出: {'songer': 'zhoujielun', 'album': 'qilixiang'} {'songer': 'chenyixun', 'album': 'meigui'} {'songer': 'naying', 'album': 'heiye'}
要求二
def make_album(songer_name,album_name,total_songs=''): '''創建一個描述音樂專輯的字典''' if total_songs: full_word = songer_name + ' ' + album_name + ' ' + total_songs else: full_word = songer_name + ' ' + album_name return full_word.title()#函數返回 mussican = make_album('zhoujielun','qilixiang') print(mussican) mussican = make_album('chenyixun','meigui') print(mussican) mussican = make_album('naying','heiye','6shou') print(mussican) 輸出: Zhoujielun Qilixiang Chenyixun Meigui Naying Heiye 6Shou
8-8
def make_album(songer_name,album_name): '''創建一個描述音樂專輯的字典''' full_ablum = {'songer_name':songer_name,'album_name':album_name}#是這個函數的定義 return full_ablum #函數的信息返回 while True: print('音樂專輯') print("(enter 'q' at any time to quit)") songer_name = input('請輸入歌手名:')#注意是每個判斷,不是到下面之后再判斷 if songer_name == 'q': break album_name = input('請輸入專輯名:') if album_name == 'q': break print(make_album(songer_name,album_name))#自己對整個語句的理解不透徹,導致此處錯誤一直沒弄對,最后這里的打印也是在while循環里面 輸出; 音樂專輯 (enter 'q' at any time to quit) 請輸入歌手名:zhoujielun 請輸入專輯名:qilixiang {'songer_name': 'zhoujielun', 'album_name': 'qilixiang'} 音樂專輯 (enter 'q' at any time to quit) 請輸入歌手名:q
8-9 魔術師 : 創建一個包含魔術師名字的列表, 並將其傳遞給一個名為show_magicians( ) 的函數, 這個函數打印列表中每個魔術師的名字。
8-10 了不起的魔術師 : 在你為完成練習 8-9而編寫的程序中, 編寫一個名為make_great( ) 的函數, 對魔術師列表進行修改, 在每個魔術師的名字中都加入字樣“the
Great”。 調用函數show_magicians( ) , 確認魔術師列表確實變了。
8-11 不變的魔術師 : 修改你為完成練習 8-10而編寫的程序, 在調用函數make_great( ) 時, 向它傳遞魔術師列表的副本。 由於不想修改原始列表, 請返回修改后的
列表, 並將其存儲到另一個列表中。 分別使用這兩個列表來調用show_magicians( ) , 確認一個列表包含的是原來的魔術師名字, 而另一個列表包含的是添加了字
樣“the Great”的魔術師名字。
8-9
def show_magicians(names): '''打印列表中每個魔術師的名字。''' for name in names: msg = ('Hello, ' + name + '!') print(msg) #print('Hello' + name),這樣打印不出來,必須定義一個函數,其實也行的 names=['caofu','wangshi','xijinp'] show_magicians(names) 輸出: Hello, caofu! Hello, wangshi! Hello, xijinp!
8-10
magicians = ['caofu','wangshi','xijinp'] great_magicians = [] def make_great(magicians,great_magicians): while magicians: magician = magicians.pop() great_magician = 'the Great ' + magician great_magicians.append(great_magician) def show_magicianslis(great_magicians): for name in great_magicians: print(name) make_great(magicians,great_magicians) show_magicianslis(great_magicians)#自己確實沒有做好 輸出:the Great xijinp the Great wangshi the Great caofu
8-11
magicians = ['caofu','wangshi','xijinp'] great_magicians = [] def make_great(magicians,great_magicians): while magicians: magician = magicians.pop() great_magician = 'the Great ' + magician great_magicians.append(great_magician) def show_magicianslis(great_magicians): for name in great_magicians: print(name) make_great(magicians[:],great_magicians)#在magicians后添加[:]啊,因為之前是這個列表有數字啊 show_magicianslis(great_magicians)# show_magicianslis(magicians)#意思在函數1里調用的只是magicians的副本 輸出; the Great xijinp the Great wangshi the Great caofu caofu wangshi xijinp
8-12 三明治 : 編寫一個函數, 它接受顧客要在三明治中添加的一系列食材。 這個函數只有一個形參(它收集函數調用中提供的所有食材) , 並打印一條消息, 對顧客
點的三明治進行概述。 調用這個函數三次, 每次都提供不同數量的實參。
8-13 用戶簡介 : 復制前面的程序user_profile.py, 在其中調用build_profile( ) 來創建有關你的簡介; 調用這個函數時, 指定你的名和姓, 以及三個描述你的鍵-值
對。
8-14 汽車 : 編寫一個函數, 將一輛汽車的信息存儲在一個字典中。 這個函數總是接受制造商和型號, 還接受任意數量的關鍵字實參。 這樣調用這個函數: 提供必不可
少的信息, 以及兩個名稱—值對, 如顏色和選裝配件。 這個函數必須能夠像下面這樣進行調用:
car = make_car(' subaru' , ' outback' , color=' blue' , tow_package=True)
答:8-12
def sandwichs(*toppings): '''接受顧客要在三明治中添加的一系列食材''' print (toppings) sandwichs('咖喱') sandwichs('牛肉','雞腿') sandwichs('咖喱','牛肉','雞腿') 輸出: ('咖喱',) ('牛肉', '雞腿') ('咖喱', '牛肉', '雞腿')
8-13
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('cao' , 'fu' , location='hefei',field='finance', character='better',ability='good', appearance='shuai',iq='gao') print(user_profile) 輸出: {'first_name': 'cao', 'last_name': 'fu', 'location': 'hefei', 'field': 'finance', 'character': 'better', 'ability': 'good', 'appearance': 'shuai', 'iq': 'gao'}
8-14
def car(manufacturers,size,**user_inf): '''總是接受制造商和型號, 還接受任意數量的關鍵字實參''' profile={}#profile 的空字典, 用於存儲用戶簡介。 profile['manu'] = manufacturers profile['si'] = size for key,value in user_inf.items():#我們遍歷字典user_info 中的鍵—值對, 並將每個鍵—值對都加入到字典profile 中 profile[key]=value return profile user=car('baoma','yueye', nenghao='good',youhao='yiban', 舒適度='好',寬敞度='大') print (user) 輸出: {'manu': 'baoma', 'si': 'yueye', 'nenghao': 'good', 'youhao': 'yiban', '舒適度': '好', '寬敞度': '大'}
整個邏輯這樣的先設置好 car(manufacturers,size,**user_inf):,定義的函數是下面的其實是profile{第一個為car中的manufactures,第二個為size,第三個就是在函數中的那個user_inf了)然后形參不是第一行就知道了么,最后再輸入car中的數據,將其返還給profile,也就是定義的函數,那給了x值定義的函數就返還出來y值
8-15 打印模型 : 將示例print_models.py中的函數放在另一個名為printing_functions.py的文件中; 在print_models.py的開頭編寫一條import 語句, 並修改這個文件以使用導
入的函數。
8-16 導入 : 選擇一個你編寫的且只包含一個函數的程序, 並將這個函數放在另一個文件中。 在主程序文件中, 使用下述各種方法導入這個函數, 再調用它:
import module_name from module_name import function_name from module_name import function_name as fn import module_name as mn from module_name import *
答:8-15
#printing_functions.py def print_models(unprinted_designs, completed_models): """ 模擬打印每個設計,直到沒有未打印的設計為止 打印每個設計后,都將其移到列表completed_models中 """ while unprinted_designs: current_design = unprinted_designs.pop() # 模擬根據設計制作3D打印模型的過程 print("Printing model: " + current_design) completed_models.append(current_design) def show_completed_models(completed_models): """顯示打印好的所有模型""" print("\nThe following models have been printed:") for completed_model in completed_models: print(completed_model) ---------------------------------------------- #print_models.py from printing_functions import * unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron'] completed_models = [] print_models(unprinted_designs, completed_models) show_completed_models(completed_models)
輸出:
Printing model: dodecahedron
Printing model: robot pendant
Printing model: iphone case
The following models have been printed:
dodecahedron
robot pendant
iphone case
8-16
#displaying.py def display_message(): print('本章學習的函數') ---------------------------------------------- import displaying displaying.display_message() from displaying import display_message display_message() from displaying import display_message as dm dm() import displaying as dm dm.display_message() from displaying import * display_message()