《Python编程:从入门到实践》 第8章习题
#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 略