2.23

 

 

 

  1 8.2.3
  2 def describe_pet(pet_name, animal_type='dog'):
  3     """显示宠物的信息"""
  4     print("\nI have a " + animal_type + ".")
  5     print("My " + animal_type + "'s name is " + pet_name.title() + ".")
  6 
  7 describe_pet(pet_name='willie')
  8 """这里修改了函数describe_pet() 的定义,在其中给形参animal_type 指定了默认值'dog' 。
  9 这样,调用这个函数时,如果没有给animal_type 指定值,Python将把这个形参设置为'dog
 10 """
 11 def make_shirt(size,print_words = "\"Hello World\""):
 12     """接收T恤的尺码和印刷字样"""
 13     print("\nYour T-shirt\'s size is " + size + ".")
 14     print(print_words + ' will be print on it.')
 15 make_shirt(size = 'X' )
 16 make_shirt(size = 'X',print_words = '\'Phyllis,Bye.\'')
 17 
 18 def make_shirt2(size = "x", print_words2):  # non-default argument follows default argument
 19     '''接收默认X码T恤'''
 20     print("Your " + size + " size T-shirt will be print" + print_words2)
 21 
 22 
 23 make_shirt2(print_words2='I love Python.')
 24 
 25 def make_shirt(print_words,size = 'x',): #重点 默认形参必须放在后面,
 26     """接收T恤的尺码和印刷字样"""
 27     print("\nYour T-shirt\'s size is " + size + ".")
 28     print(print_words + ' will be print on it.')
 29 make_shirt(print_words = "\"Hello World\"")
 30 make_shirt(print_words = '\'Phyllis,Bye.\'')
 31 
 32 def cities_in_countries(city, country="Japan"):  # describe_city()
 33     print(city + " is in " + country)
 34 
 35 
 36 cities_in_countries("Osaka")
 37 cities_in_countries("Tokyo")
 38 cities_in_countries("Shanghai", country="China")
 39 
 40 
 41 def get_formatted_name(first_name, last_name, middle_name=" "):
 42     # print(first_name.title + middle_name + last_name.title) # 应先将形参储存至变量
 43     if middle_name:
 44         full_name = first_name + " " + middle_name + " " + last_name
 45     else:
 46         full_name = first_name + middle_name + last_name
 47     return full_name.title()
 48 
 49 
 50 goddess = get_formatted_name("phylls", "ryo")
 51 print(goddess)
 52 test = get_formatted_name("John", "Hooker", "Lee")
 53 print(test)
 54 
 55 # 练习8-6
 56 def city_country(city, country):
 57     """输出城市及其所属国家"""
 58     print(city.title(), country.title())
 59 
 60 
 61 city_country("tokyo", "Japan")
 62 """8-7 专辑 :编写一个名为make_album() 的函数,它创建一个描述音乐专辑的字典。这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典。使
 63 用这个函数创建三个表示不同专辑的字典,并打印每个返回的值,以核实字典正确地存储了专辑的信息。
 64 给函数make_album() 添加一个可选形参,以便能够存储专辑包含的歌曲数。如果调用这个函数时指定了歌曲数,就将这个值添加到表示专辑的字典中。调用这个
 65 函数,并至少在一次调用中指定专辑包含的歌曲数。
 66 8-8 用户的专辑 :在为完成练习8-7编写的程序中,编写一个while 循环,让用户输入一个专辑的歌手和名称。获取这些信息后,使用它们来调用函
 67 数make_album() ,并将创建的字典打印出来。在这个while 循环中,务必要提供退出途径。"""
 68 def make_album(singer, album, songs=''):
 69     music_album = {'singer': singer.title(), 'album': album.title()}
 70     if songs:
 71         # music_album = {'singer': singer, 'album': album, 'songs': int(songs)}
 72         # 此处应使用为字典添加键值功能 字典定义应该且移到if语句之外
 73         music_album['songs'] = int(songs) #错把字典名music_album 打成函数名make_album
 74     # else: # 多卵余
 75     #     music_album = {'singer':singer.title(), 'album':album.title()}
 76     return music_album #不可以在此处使用.title()  'dict' object has no attribute 'title'
 77 
 78 
 79 print(make_album('Jay Zhou', 'Fantasy'))
 80 print(make_album('Jacky Zhang','wake up dreaming',10))
 81 print(make_album('eason chen','cheerful on the fifth floor.','11'))
 82 
 83 def make_album(singer, album, songs=''):
 84     music_album = {'singer': singer.title(), 'album': album.title()}
 85     if songs:
 86         music_album['songs'] = int(songs)
 87     return music_album
 88 
 89 
 90 while True:
 91     print("\nPlease tell me your favorite singer and his/her album.")
 92     print("(enter \'q\' at any time to quit)")
 93 
 94     singer = input('Sing\'s name: ')
 95     if singer == 'q':
 96         break
 97     album = input('His/Her album: ')
 98     if album == 'q':
 99         break
100     print(make_album(singer,album))
101 
102 切片表示法[:] 创建列表的副本。在print_models.py中,如果不想清空未打印的设计列表,可像下面这样调用print_models() :
103 print_models(unprinted_designs[:], completed_models)
104 
105 """练习8-9
106 8-9 魔术师 :创建一个包含魔术师名字的列表,并将其传递给一个名为show_magicians() 的函数,这个函数打印列表中每个魔术师的名字。 # 问题 
107 8-10 #问题 了不起的魔术师 :在你为完成练习8-9而编写的程序中,编写一个名为make_great() 的函数,对魔术师列表进行修改,在每个魔术师的名字中都加入字样“the
108 Great”。调用函数show_magicians() ,确认魔术师列表确实变了。
109 8-11 不变的魔术师 :修改你为完成练习8-10而编写的程序,在调用函数make_great() 时,向它传递魔术师列表的副本。由于不想修改原始列表,请返回修改后的
110 列表,并将其存储到另一个列表中。分别使用这两个列表来调用show_magicians() ,确认一个列表包含的是原来的魔术师名字,而另一个列表包含的是添加了字
111 样“the Great”的魔术师名字。
112 """
113 
114 def show_magicians(magicians_names):
115     """Print magicians's names."""
116     for name in magicians_names:
117         print(name.title())
118 
119 
120 def make_great(magicians_names):
121     """Make magicians great."""
122     while magicians_names: #应该在调用函数时使用副本
123         name_add = "the Great " + magicians_names.pop().title()
124         great_magicians.append(name_add)
125 
126 magicians_names = ["phylls", "shiitakeimoto", "soso", "ergou", "erzi", "momoe"]
127 great_magicians = []
128 
129 make_great(magicians_names[:])
130 print(magicians_names)
131 print(great_magicians)
View Code

 

posted @ 2019-02-24 02:12  波波皮球  阅读(99)  评论(0编辑  收藏  举报