第八章python作業

8-3 T恤 :編寫一個名爲make_shirt() 的函數,它接受一個尺碼以及要印到T恤上的字樣。這個函數應打印一個句子,概要地說明T恤的尺碼和字樣。
def make_shirt(size,word):
    print("The size of this T-shirt:"+size)
    print("The word of this T-shirt:"+word+'\n')
make_shirt("XL","Hello world!")
make_shirt(word="I love apple!",size='XXL')

8-4 大號T恤 :修改函數make_shirt() ,使其在默認情況下製作一件印有字樣“I love Python”的大號T恤。調用這個函數來製作如下T恤:一件印有默認字樣的大號T恤、一件印有默認字樣的中號T恤和一件印有其他字樣的T恤(尺碼無關緊要)。

def make_shirt(size='L',word="I love Python"):
    print("The size of this T-shirt:"+size)
    print("The word of this T-shirt:"+word+'\n')
make_shirt()
make_shirt('M')
make_shirt('S',"Hello world!")

8-5 城市 :編寫一個名爲describe_city() 的函數,它接受一座城市的名字以及該城市所屬的國家。這個函數應打印一個簡單的句子,如Reykjavik is in Iceland 。給用於存儲國家的形參指定默認值。爲三座不同的城市調用這個函數,且其中至少有一座城市不屬於默認國家。

def describe_city(city,country='China'):
    print(city+' is in '+country)
describe_city('Beijing')
describe_city('Guangzhou')
describe_city("London",'UK')
describe_city("Paris",'France')
8-6 城市名 :編寫一個名爲city_country() 的函數,它接受城市的名稱及其所屬的國家。這個函數應返回一個格式類似於下面這樣的字符串:"Santiago, Chile"

至少使用三個城市-國家對調用這個函數,並打印它返回的值。

def city_country(city,country):
    return city+','+country
print(city_country("Guangzhou","China"))
print(city_country("Lodon","UK"))
print(city_country("New York","USA"))
8-7 專輯 :編寫一個名爲make_album() 的函數,它創建一個描述音樂專輯的字典。這個函數應接受歌手的名字和專輯名,並返回一個包含這兩項信息的字典。使用這個函數創建三個表示不同專輯的字典,並打印每個返回的值,以覈實字典正確地存儲了專輯的信息。

給函數make_album() 添加一個可選形參,以便能夠存儲專輯包含的歌曲數。如果調用這個函數時指定了歌曲數,就將這個值添加到表示專輯的字典中。調用這個函數,並至少在一次調用中指定專輯包含的歌曲數。

def make_album(singer,name,number=''):
    a={'singer':singer,'name':name}
    if (number):
         a['number']=number
    return a
print(make_album('Taylor Swift','1989',str(13)))
print(make_album('Jay Chou','November\'s Chopin'))
print(make_album('A new heartbeat','G.E.M'))

8-9 魔術師 :創建一個包含魔術師名字的列表,並將其傳遞給一個名爲show_magicians() 的函數,這個函數打印列表中每個魔術師的名字。

def show_magicians(mlist):
    for magicican in mlist:
        print(magicican)
magicicans=['David Copperfield','David Blaine','Jason Latimer']
show_magicians(magicicans)

8-10 了不起的魔術師 :在你爲完成練習8-9而編寫的程序中,編寫一個名爲make_great() 的函數,對魔術師列表進行修改,在每個魔術師的名字中都加入字樣“the Great”。調用函數show_magicians() ,確認魔術師列表確實變了。

def show_magicians(mlist):
    for magicican in mlist:
        print(magicican)
def make_great(mlist):
    length=len(mlist)
    for i in range(1,length+1):
        mlist[i-1]='the Great '+mlist[i-1]
magicicans=['David Copperfield','David Blaine','Jason Latimer']
make_great(magicicans)
show_magicians(magicicans)

8-11 不變的魔術師 :修改你爲完成練習8-10而編寫的程序,在調用函數make_great() 時,向它傳遞魔術師列表的副本。由於不想修改原始列表,請返回修改後的列表,並將其存儲到另一個列表中。分別使用這兩個列表來調用show_magicians() ,確認一個列表包含的是原來的魔術師名字,而另一個列表包含的是添加了字樣“the Great”的魔術師名字。

def show_magicians(mlist):
    for magicican in mlist:
        print(magicican)
def make_great(mlist):
    length=len(mlist)
    for i in range(1,length+1):
        mlist[i-1]='the Great '+mlist[i-1]
    return mlist
magicicans=['David Copperfield','David Blaine','Jason Latimer']
great_magiccans=make_great(magicicans[:])
show_magicians(magicicans)
show_magicians(great_magiccans)

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('Zhenhang','Zheng',age=19,Country='China',city='Guangzhou')
print(user_profile)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章