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

#8-1 消息
def display_message():
      print('本章学习的函数')
display_message()
#8-2 喜欢的图书
def favorite_book(title):
      print('One of my favorite books is ' + title.title())
favorite_book('python')
#8-3 T恤
def make_shirt(size,pattern):
      print('The size is ' + size + '\tThe pattern is ' + pattern)
make_shirt('L','cat')
#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')
#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')
#8-6 城市名
def city_country(city,country):
      message = city.title() + ',' + country.title()
      return message
print(city_country('beijing','china'))
#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 album
print(make_album('周杰伦','听妈妈的话'))
print(make_album('某某','某某某','5'))
#8-8 用户的专辑
def make_album(name,album_name,number=''):
      album = {'name':name,'album_name':album_name}
      return album
while 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))
#8-9 魔术师
magicians = ['ergou','qiqi','shitou']
def show_magicianslis(magicians):
      for name in magicians:
            print(name)
show_magicianslis(magicians)
#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)
#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)
#8-12 三明治
def sandwich(*toppings):
      print("\nMaking a sandwich with the following toppings:")
      for topping in toppings:
          print("- " + topping)

sandwich('apple')
sandwich('banana','apple')
#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 profile

user_profile = build_profile('albert','einstein',location='princeton',field='physics',)
print(user_profile)
#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 profile

car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)
#8-15 打印模型
#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)

----------------------------------------------
#print_models.py
from printing_functions import *

unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []

print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)
#8-16 导入
#displaying.py
def display_message():
      print('本章学习的函数')

----------------------------------------------
import displaying
displaying.display_message()

from displaying import display_message
display_message()

from displaying import display_message as dm
dm()

import displaying as dm
dm.display_message()

from displaying import *
display_message()
#8-17 函数编写指南
  • 24
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值