当前位置:   article > 正文

《Python编程:从入门到实践》习题答案——第8章 函数_python编程课后题第八章答案

python编程课后题第八章答案
  1. # 8-1 消息
  2. def display_message(thing):
  3. print(f"本章学习{thing}")
  4. display_message('函数')
  5. display_message('元组')
  6. # 8-2 喜欢的图书
  7. def favorite_book(title):
  8. print(f"One of my favorite books is {title}")
  9. favorite_book('Alice in Wonderland')
  10. # 8-3 T恤
  11. def make_shirt(size, title):
  12. print(f"这件T恤的尺码是:{size}号,印有'{title}'字样")
  13. make_shirt(size='大', title = 'I love Python')
  14. make_shirt('大', 'I love Python')
  15. # 8-4 大号T恤
  16. def make_shirt(size, title):
  17. print(f"制作一件印有字样'{title}'的{size}号T恤。")
  18. make_shirt('大', 'I love Python')
  19. make_shirt('中', 'I love Python')
  20. make_shirt('小', 'I love Python')
  21. # 8-5 城市
  22. def describe_city(city, nation='China'):
  23. print(f"{city} is in {nation}")
  24. describe_city(city = 'Shanghai')
  25. describe_city('Taiwan')
  26. describe_city('Reykjavik', 'Iceland')
  27. # 8-6 城市名
  28. def city_country(city, country):
  29. str1 = city + ', ' + country
  30. return str1
  31. c_c1 = city_country('Taibei', 'China')
  32. c_c2 = city_country('Reykjavik', 'Iceland')
  33. c_c3 = city_country('santiago', 'Chile')
  34. print(c_c1)
  35. print(c_c2)
  36. print(c_c3)
  37. # 8-7 专辑
  38. def make_album(singer, name, num = ''):
  39. album = {'singer':singer, 'name':name}
  40. if num:
  41. album['num'] = num
  42. return album
  43. ma1 = make_album('Lijian', 'efaf')
  44. print(ma1)
  45. ma2 = make_album('DengZiqi', 'drms', 10)
  46. print(ma2)
  47. ma3 = make_album('LiWen', 'forever', '8')
  48. print(ma3)
  49. # 8-8 用户的专辑
  50. def make_album(singer, name, num = ''):
  51. album = {'singer': singer_name, 'name': album_name, 'num':album_num}
  52. return album
  53. while True:
  54. print("Please tell singer_name,album_name,album_num.")
  55. print("enter 'q' at any time to quit")
  56. singer_name = input("singer_name:")
  57. if singer_name == 'q':
  58. break
  59. album_name = input("album_name:")
  60. if album_name == 'q':
  61. break
  62. album_num = input("album_num:")
  63. if album_num == 'q':
  64. break
  65. a = make_album(singer_name, album_name, album_num)
  66. print(a)
  67. # 8-9 魔术师
  68. def show_magicians(names):
  69. for name in names:
  70. print(name)
  71. magicians_1 = ['ming', 'tian', 'geng', 'mei', 'hao']
  72. show_magicians(magicians_1)
  73. # 8-10 了不起的魔术师
  74. def make_great(bf_names, af_names):
  75. while bf_names:
  76. current_name = bf_names.pop()
  77. current_name += 'the Great'
  78. af_names.append(current_name)
  79. def show_magicians(bf_names, af_names):
  80. print("bf_names:")
  81. for bf_name in bf_names:
  82. print(bf_name)
  83. print("\naf_names:")
  84. for af_name in af_names:
  85. print(af_name)
  86. bf_names = ['ming', 'tian', 'geng', 'mei', 'hao']
  87. af_names = []
  88. make_great(bf_names, af_names)
  89. show_magicians(bf_names, af_names)
  90. # 8-11 不变的魔术师
  91. def make_great(bf_names, af_names):
  92. while bf_names:
  93. current_name = bf_names.pop()
  94. current_name += 'the Great'
  95. af_names.append(current_name)
  96. def show_magicians(bf_names, af_names):
  97. print("bf_names:")
  98. for bf_name in bf_names:
  99. print(bf_name)
  100. print("\naf_names:")
  101. for af_name in af_names:
  102. print(af_name)
  103. bf_names = ['ming', 'tian', 'geng', 'mei', 'hao']
  104. af_names = []
  105. make_great(bf_names[:], af_names) # 添加一个副本
  106. show_magicians(bf_names, af_names)
  107. # 8-12 三明治
  108. def make_sandwich(*requests):
  109. print("\nMaking a sandwich with the following requests: ")
  110. for request in requests:
  111. print("- " + request)
  112. make_sandwich('pepperoni')
  113. make_sandwich('mushrooms', 'extra cheese')
  114. make_sandwich('mushrooms', 'green peppers', 'extra cheese')
  115. # 8-13 用户简介
  116. def build_profile(first, last, **user_info):
  117. profile = {}
  118. profile['first_name'] = first
  119. profile['last_name'] = last
  120. for key, value in user_info.items():
  121. profile[key] = value
  122. return profile
  123. user_profile = build_profile('Dashen', 'Sun',
  124. location = 'Hefei' ,
  125. university ='USTC',
  126. field = 'quantum')
  127. print(user_profile)
  128. # 8-14 汽车
  129. def make_car(brand, model, **info):
  130. profile = {}
  131. profile['brand_name'] = brand
  132. profile['model_name'] = model
  133. for k,v in info.items():
  134. profile[k] = v
  135. return profile
  136. info = make_car('BYD', 'SUV',
  137. location = 'Shenzhen',
  138. nation = 'China',
  139. color= 'yellow')
  140. print(info)
  141. # 8-15 打印模型
  142. from printing_functions import print_models as pm
  143. from printing_functions import show_completed_models as scm
  144. # from printing_functions import *
  145. unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
  146. completed_models = []
  147. pm(unprinted_designs,completed_models) # 调用函数
  148. scm(completed_models)
  149. print("\nunprinted_designs: ")
  150. print(unprinted_designs)
  151. print("\ncompleted_models:")
  152. print(completed_models)
  153. # 8-16 导入
  154. # 1.导入整个模块
  155. import build_profile123
  156. user_profile = build_profile123.build_profile('albert', 'einstein',
  157. location = 'princeton',
  158. field = 'physics')
  159. print(user_profile)
  160. # 2.导入特定的函数
  161. from build_profile123 import build_profile
  162. user_profile = build_profile('albert', 'einstein',
  163. location = 'princeton',
  164. field = 'physics')
  165. print(user_profile)
  166. # 3.使用as给函数指定别名
  167. from build_profile123 import build_profile as bp
  168. user_profile = bp('albert', 'einstein',
  169. location = 'princeton',
  170. field = 'physics')
  171. print(user_profile)
  172. # 4.使用as给模块指定别名
  173. import build_profile123 as bp123
  174. user_profile = bp123.build_profile('albert', 'einstein',
  175. location = 'princeton',
  176. field = 'physics')
  177. print(user_profile)
  178. # 5.导入模块中的所有函数
  179. from build_profile123 import *
  180. user_profile = build_profile('albert', 'einstein',
  181. location = 'princeton',
  182. field = 'physics')
  183. print(user_profile)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/616903
推荐阅读
相关标签
  

闽ICP备14008679号