#8-1消息:編寫一個名為display_message()的函數,它列印一個句子,指出你
#在本章學的是什麼。調用這個函數,确認顯示的消息正确無誤。
def display_message():
print("你正在學的是第八章,函數。")
display_message()
#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("calabash brothers")
#8-3T恤:編寫一個名為make_shirt()的函數,它接受一個尺碼以及要印到T恤上
#的字樣。這個函數應列印一個句子,概要地說明T恤的尺碼和字樣。
def make_shirt(size, logo):
print("\nThe dress is in size " + str(size) + ".")
print("The inscription on The t-shirt is" + logo.title())
make_shirt(25, 'The dog egg')
make_shirt(logo = 'The cat egg', size = 26)
#8-4大号T恤:修改函數make_shirt(),使其在預設情況下制作一件印有字樣“I
#lovePython”的大号T恤。調用這個函數來制作如下T恤:一件印有預設字樣的大号T恤、
#一件印有預設字樣的中号T恤和一件印有其他字樣的T恤(尺碼無關緊要)。
def make_shirt(size, logo = 'I love Python'):
print("\nThe dress is in size " + str(size) + ".")
print("The inscription on The t-shirt is" + logo.title())
make_shirt(size = 'x')
make_shirt(size = 'l')
make_shirt('l', logo = 'The dog egg')
#8-5城市:編寫一個名為describe_city()的函數,它接受一座城市的名字以及該城市所屬的
#國家。這個函數應列印一個簡單的句子,如Reykjavik is in Iceland。給用于存儲國家
#的形參指定預設值。為三座不同的城市調用這個函數,且其中至少有一座城市不屬于預設國家。
def describe_city(name, country = 'US'):
print(name.title() + " is in " + country.title())
describe_city('washington')
describe_city('chengdu', country = 'china')
describe_city(name = 'paris', country = 'france')
#8-6 城市名:編寫一個名為city_country()的函數,它接受城市的名稱及其所屬的
#國家。這個函數應傳回一個格式類似于下面這樣的字元串:"Santiago, Chile"
def city_country(name, country):
full_name = name + ' ' + country
return full_name.title()
location = city_country('beijing', 'chine')
print(location)
location = city_country('Gibraltar', 'england')
print(location)
location = city_country('copenhagen', 'Denmark')
print(location)
#8-7專輯:編寫一個名為make_album()的函數,它建立一個描述音樂專輯的字典。這個函數
#應接受歌手的名字和專輯名,并傳回一個包含這兩項資訊的字典。使用這個函數建立三個表示
#不同專輯的字典,并列印每個傳回的值,以核實字典正确地存儲了專輯的資訊。
def make_album(singerName, albumName, songsNumber = ''):
if songsNumber:
summarize = {
'name':singerName,
'album':albumName,
'number':songsNumber
}
else:
summarize = {'name':singerName, 'album':albumName}
return summarize
albumSummarize = make_album('Nicolas Errera', 'The Butterfly')
print(albumSummarize)
albumSummarize = make_album('muse', 'Unintended',12)
print(albumSummarize)
albumSummarize = make_album('The Beatles', 'Hey Jude',9)
print(albumSummarize)
#8-8使用者的專輯:在為完成練習8-7編寫的程式中,編寫一個while循環,讓使用者輸
#入一個專輯的歌手和名稱。擷取這些資訊後,使用它們來調用函數make_album(),
#并将建立的字典列印出來。在這個while循環中,務必要提供退出途徑.
def make_album(singerName, albumName, songsNumber = ''):
if songsNumber:
summarize = {
'name':singerName,
'album':albumName,
'numbers':songsNumber
}
else:
summarize = {'name':singerName, 'album':albumName}
return summarize
while True:
singer = input("輸入歌手名字(輸入'q'即可退出)")
if singer == 'q':
break
album = input("輸入專輯名字(輸入'q'即可退出)")
if album == 'q':
break
number = input("輸入歌曲數量(輸入'q'即可退出)")
if number == 'q':
break
albumSummarize = make_album(singer, album, number)
print(albumSummarize)
#8-9魔術師:建立一個包含魔術師名字的清單,并将其傳遞給一個名
#為show_magicians()的函數,這個函數列印清單中每個魔術師的名字。
def shou_magicians(names):
for name in names:
print(name)
magicians = ['wwz','zzq','llo']
shou_magicians(magicians)
#8-10了不起的魔術師:在你為完成練習8-9而編寫的程式中,編寫一個名
#為make_great()的函數,對魔術師清單進行修改,在每個魔術師的名字中都加入
#字樣“theGreat”。調用函數show_magicians(),确認魔術師清單确實變了。
def make_great(names, roll):
while names:
name = names.pop()
print(name + ' the great.')
roll.append(name)
magicians_names = ['wwz','zzq','llo']
roll_names = []
make_great(magicians_names, roll_names)
print(magicians_names)
print(roll_names)
#8-12 三明治:編寫一個函數,它接受顧客要在三明治中添加的一系列食材。這個函數隻
#有一個形參(它收集函數調用中提供的所有食材),并列印一條消息,對顧客點的三明治
#進行概述。調用這個函數三次,每次都提供不同數量的實參。
def add_ingredients(ingredients):
print('The side dish you add to your sandwich is ' +
ingredients.title())
add_ingredients('egg')
add_ingredients('beef')
add_ingredients('cheese')
#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('w', 'zc',
location='guangyuan',
field='game',
gender='man')
print(user_profile)
#8-14 汽車:編寫一個函數,将一輛汽車的資訊存儲在一個字典中。這個函數總是接受制造
#商和型号,還接受任意數量的關鍵字實參。這樣調用這個函數:提供必不可少的資訊,以及
#兩個名稱—值對,如顔色和選裝配件。這個函數必須能夠像下面這樣進行調用:
def make_car(manufacturer, model, **number):
profile = {}
profile['制造商'] = manufacturer
profile['型号'] = model
for key, value in number.items():
profile[key] = value
return profile
car = make_car(
'寶馬',
'越野',
顔色='銀色',
配置='中配',
安全性='優',
舒适性='5星',
)
print(car)
#8-15 列印模型:将示例print_models py中的函數放在另一個名為printing_functions.py
#的檔案中;在print_models.py的開頭編寫一條import語句,并修改這個檔案以使用導入的函數。
''' # 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)'''
import printing_functions as spam
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
spam.print_models(unprinted_designs, completed_models)
spam.show_completed_models(completed_models)
#8-16
略
#8-17
略