您的位置:首页 > 编程语言 > Python开发

第八章python作业

2018-03-31 15:13 225 查看
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: