Python 從入門到實踐 8 6 課後習題

2021-08-13 17:29:18 字數 3053 閱讀 8571

8.6

城市名:編寫乙個名為city_country()的函式,它接受城市的名稱及其所屬的

國家。這個函式應返回乙個格式類似於下面這樣的字串:

defcity_country(city, country):

full_city_country = city +','+ country

returnfull_city_country.title()

while true:

print("\nplease tell me a city")

print("(enter 'q' at any time to quit)")

ci_name = input("city:")

ifci_name =='q':

breakprint("\ntell me the country of this city:")

print("(enter 'q' at any time to quit)")

co_name = input("counrty:")

ifco_name =='q':

breakcity_country_1 = city_country(ci_name, co_name)

print(city_country_1)

8.7 

**:編寫乙個名為make_album()的函式,它建立乙個描述****的字典。

這個函式應接受歌手的名字和**名,並返回乙個包含這兩項資訊的字典。使用這個函

數建立三個表示不同**的字典,並列印每個返回的值,以核實字典正確地儲存了**

的資訊。

給函式make_album()新增乙個可選形參,以便能夠儲存**包含的歌曲數。如果呼叫

這個函式時指定了歌曲數,就將這個值新增到表示**的字典中。呼叫這個函式,並

至少在一次呼叫中指定**包含的歌曲數。

defmake_albm(name_name, albm_name, number=''):

albm =

ifnumber:

albm['number'] = number

returnalbm

while true:

counter = 0

ifcounter ==3:

breakelse:

print("please tell me three albm which you very like:")

n_name = input("the singer name:")

a_name = input("the albm name:")

counter +=1

full_name = make_albm(n_name,a_name)

print(full_name)

8.8使用者的**:在為完成練習8-7 編寫的程式中,編寫乙個while 迴圈,讓使用者

輸入乙個**的歌手和名稱。獲取這些資訊後,使用它們來呼叫函式make_album(),並

將建立的字典列印出來。在這個while 迴圈中,務必要提供退出途徑。

defmake_albm(name_name, albm_name, number=''):

albm =

ifnumber:

albm['number'] = number

returnalbm

while true:

counter = 0

ifcounter ==3:

breakelse:

print("please tell me three albm which you very like:")

print("(enter 'q' at any time to quit)")

n_name = input("the singer name:")

ifn_name =='q':

breaka_name = input("the albm name:")

ifa_name =='q':

breakcounter +=1

full_name = make_albm(n_name,a_name)

print(full_name)

python從入門到實踐

1.類,例項化是根據類來建立物件 類的實踐 建立乙個人類 class humans 3.7版本不加括號 def init self,name,height 血的教訓init前後兩條下劃線,self不是類的關鍵字 self.name name self.self.height height def l...

python從入門到實踐

1.json初探 import json json模組初探 json在pytho之間分享資料 json.dump a,b 要儲存的資料和用於儲存資料的檔案物件 json.load 將資料載入記憶體 number 1,2,3,4,5,6,7 filename number.json 副檔名.json指...

Python 從入門到實踐 6 1 課後習題

6.1 人 使用乙個字典來儲存乙個熟人的資訊,包括名 姓 年齡和居住的城市。該字典應包含鍵first name last name age 和city。將儲存在該字典中的每項資訊都 列印出來。message print the information of str message first nam...