Python编程:从入门到实践的动手试一试(第八章)

来源:互联网 发布:授权回调页面域名 编辑:程序博客网 时间:2024/06/16 04:22
#8-1 消息def display_message():      print('本章学习的函数')display_message()
  • 1
  • 2
  • 3
  • 4
#8-2 喜欢的图书def favorite_book(title):      print('One of my favorite books is ' + title.title())favorite_book('python')
  • 1
  • 2
  • 3
  • 4
#8-3 T恤def make_shirt(size,pattern):      print('The size is ' + size + '\tThe pattern is ' + pattern)make_shirt('L','cat')
  • 1
  • 2
  • 3
  • 4
#8-4 大号T恤def make_shirt(size,pattern = 'I love Python'):      print('The size is ' + size + '\tThe pattern is ' + pattern)make_shirt('XL')make_shirt('L')make_shirt('XL','cat')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
#8-5 城市def describe_city(city,country='China'):      print(city.title() + ' is in ' + country.title())describe_city('beijing')describe_city('qingdao')describe_city('new york','USA')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
#8-6 城市名def city_country(city,country):      message = city.title() + ',' + country.title()      return messageprint(city_country('beijing','china'))
  • 1
  • 2
  • 3
  • 4
  • 5
#8-7 专辑def make_album(name,album_name,number = ''):      if number:            album = {'name':name,'album_name':album_name,'number':number}      else:            album = {'name':name,'album_name':album_name}      return albumprint(make_album('周杰伦','听妈妈的话'))print(make_album('某某','某某某','5'))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
#8-8 用户的专辑def make_album(name,album_name,number=''):      album = {'name':name,'album_name':album_name}      return albumwhile True:      print("(enter 'q' at any time to quit)")      name = input('请输入歌手名:')      if name == 'q':            break      album_name = input('请输入专辑名:')      if album_name == 'q':            break      print(make_album(name,album_name))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
#8-9 魔术师magicians = ['ergou','qiqi','shitou']def show_magicianslis(magicians):      for name in magicians:            print(name)show_magicianslis(magicians)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
#8-10 了不起的魔术师magicians = ['ergou','qiqi','shitou']great_magicians = []def make_great(magicians,great_magicians):      while magicians:            magician = magicians.pop()            great_magician = 'the Great ' + magician             great_magicians.append(great_magician)def show_magicianslis(great_magicians):      for name in great_magicians:            print(name)make_great(magicians,great_magicians)show_magicianslis(great_magicians)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
#8-11 不变的魔术师magicians = ['ergou','qiqi','shitou']great_magicians = []def make_great(magicians,great_magicians):      while magicians:            magician = magicians.pop()            great_magician = 'the Great ' + magician             great_magicians.append(great_magician)def show_magicianslis(great_magicians):      for name in great_magicians:            print(name)make_great(magicians[:],great_magicians)show_magicianslis(great_magicians)show_magicianslis(magicians)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
#8-12 三明治def sandwich(*toppings):      print("\nMaking a sandwich with the following toppings:")      for topping in toppings:          print("- " + topping)sandwich('apple')sandwich('banana','apple')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
#8-13 用户简介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 profileuser_profile = build_profile('albert','einstein',location='princeton',field='physics',)print(user_profile)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
#8-14 汽车def make_car(manufacturer, model, **type_info):      """创建一个字典,其中包含我们知道的有关用户的一切"""      profile = {}      profile['manufacturer'] = manufacturer      profile['model'] = model      for key, value in type_info.items():          profile[key] = value      return profilecar = make_car('subaru', 'outback', color='blue', tow_package=True)print(car)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
#8-15 打印模型#printing_functions.pydef 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)----------------------------------------------#print_models.pyfrom printing_functions import *unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']completed_models = []print_models(unprinted_designs, completed_models)show_completed_models(completed_models)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
#8-16 导入#displaying.pydef display_message():      print('本章学习的函数')----------------------------------------------import displayingdisplaying.display_message()from displaying import display_messagedisplay_message()from displaying import display_message as dmdm()import displaying as dmdm.display_message()from displaying import *display_message()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
#8-17 函数编写指南
阅读全文
0 0
原创粉丝点击