8-2 喜欢的图书:编写一个名为favorite_book()的函数,其中包含一个名为title的形参。这个函数打印一条消息,如One of my favorite books is Alice in Wonderland。调用这个函数,并将一本图书的名称作为实参传递给它。

def favorite_book(book):print('One of my favorite books is ' + book + '.')favorite_book('Ordinary World')

输出:

One of my favorite books is Ordinary World.

 
8-3 T恤:编写一个名为make_shirt()的函数,它接受一个尺码以及要印到T恤上的字样。这个函数应打印一个句子,概要地说明T恤的尺码和字样。使用位置实参调用这个函数来制作一件T恤;再使用关键字实参来调用这个函数。

def make_shirt(size, word):print('The size of shirt is ' + size + '.')print('The words printed on the shirt are: ' + word)make_shirt('XL', 'I love Python!')
make_shirt(word='I love Python!', size='XL')

输出:

The size of shirt is XL.
The words printed on the shirt are: I love Python!
The size of shirt is XL.
The words printed on the shirt are: I love Python!

 
8-5 城市:编写一个名为describe_city()的函数,它接受一座城市的名字以及该城市所属的国家。这个函数应打印一个简单的句子,如Reykjavik is in Iceland。给用于存储国家的形参指定默认值。为三座不同的城市调用这个函数,且其中至少有一座城市不属于默认国家。

def describe_city(city, country='China'):print(city.title() + ' is in ' + country.title() + '.')describe_city('Beijing')
describe_city('Guangzhou')
describe_city('new york', 'america')

输出:

Beijing is in China.
Guangzhou is in China.
New York is in America.

 
8-7 专辑:编写一个名为make_album()的函数,它创建一个描述音乐专辑的字典。这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典。使用这个函数创建三个表示不同专辑的字典,并打印每个返回的值,以核实字典正确地存储了专辑的信息。
给函数make_album()添加一个可选形参,以便能够存储专辑包含的歌曲数。如果调用这个函数时指定了歌曲数,就将这个值添加到表示专辑的字典中。调用这个函数,并至少在一次调用中指定专辑包含的歌曲数。
8-8 用户的专辑:在为完成练习8-7编写的程序中,编写一个while循环,让用户输入一个专辑的歌手和名称。获取这些信息后,使用它们来调用函数make_album(),并将创建的字典打印出来。在这个while循环中,务必要提供退出途径。

def make_album(singer, album, song_num = 12):return {'singer': singer, 'album': album, 'song_num': song_num}
print(make_album('Taylor Swift', '1989', 13))
print(make_album('Troye Sivan', 'Blue Neighbourhood', 18))
print(make_album('Ed Sheeran', '÷'))while True:singer = input('\nPlease enter a singer: ')album = input('Please enter the name of his/her album: ')print(make_album(singer, album))message = input("Enter 'quit' when you are finished, or enter 'no': ")if message == 'quit':break

输入:

Ed Sheeran
÷
quit

输出:

{'song_num': 13, 'singer': 'Taylor Swift', 'album': '1989'}
{'song_num': 18, 'singer': 'Troye Sivan', 'album': 'Blue Neighbourhood'}
{'song_num': 12, 'singer': 'Ed Sheeran', 'album': '÷'}Please enter a singer: Ed Sheeran
Please enter the name of his/her album: ÷
{'song_num': 12, 'singer': 'Ed Sheeran', 'album': '÷'}
Enter 'quit' when you are finished, or enter 'no': quit

 
8-9 魔术师:创建一个包含魔术师名字的列表,并将其传递给一个名为show_magicians()的函数,这个函数打印列表中每个魔术师的名字。

def show_magicians(magicians):for magician in magicians:print(magician)show_magicians(['Criss Angel', 'David Copperfield', 'Jason Latimer'])

输出:

Criss Angel
David Copperfield
Jason Latimer

 
8-10 了不起的魔术师:在你为完成练习8-9而编写的程序中,编写一个名为make_great()的函数,对魔术师列表进行修改,在每个魔术师的名字中都加入字样“the Great”。调用函数show_magicians(),确认魔术师列表确实变了。

def show_magicians(magicians):for magician in magicians:print(magician)def make_great(magicians):for num in range(len(magicians)):magicians[num] = 'the Great ' + magicians[num] magicians = ['Criss Angel', 'David Copperfield', 'Jason Latimer']
make_great(magicians)
show_magicians(magicians)

输出:

the Great Criss Angel
the Great David Copperfield
the Great Jason Latimer

 
8-11 不变的魔术师:修改你为完成练习8-10而编写的程序,在调用函数make_great()时,向它传递魔术师列表的副本。由于不想修改原始列表,请返回修改后的列表,并将其存储到另一个列表中。分别使用这两个列表来调用show_magicians(),确认一个列表包含的是原来的魔术师名字,而另一个列表包含的是添加了字样“the Great”的魔术师名字。

def show_magicians(magicians):for magician in magicians:print(magician)def make_great(magicians):for num in range(len(magicians)):magicians[num] = 'the Great ' + magicians[num]return magiciansmagicians = ['Criss Angel', 'David Copperfield', 'Jason Latimer']
new_magicians = make_great(magicians[:])
show_magicians(magicians)
show_magicians(new_magicians)

输出:

Criss Angel
David Copperfield
Jason Latimer
the Great Criss Angel
the Great David Copperfield
the Great Jason Latimer

 
8-12 三明治:编写一个函数,它接受顾客要在三明治中添加的一系列食材。这个函数只有一个形参(它收集函数调用中提供的所有食材),并打印一条消息,对顾客点的三明治进行概述。调用这个函数三次,每次都提供不同数量的实参。

def print_ingredients(*ingredients):print("The sandwich include the following ingredients:")for ingredient in ingredients:print('- ' + ingredient)print()print_ingredients('cheese')
print_ingredients('sausage', 'cheese')
print_ingredients('lettuce', 'sausage', 'cheese')

输出:

The sandwich include the following ingredients:
- cheeseThe sandwich include the following ingredients:
- sausage
- cheeseThe sandwich include the following ingredients:
- lettuce
- sausage
- cheese

 
8-14 汽车:编写一个函数,将一辆汽车的信息存储在一个字典中。这个函数总是接受制造商和型号,还接受任意数量的关键字实参。这样调用这个函数:提供必不可少的信息,以及两个名称—值对,如颜色和选装配件。这个函数必须能够像下面这样进行调用:

car = make_car('subaru', 'outback', color='blue', tow_package=True)

打印返回的字典,确认正确地处理了所有的信息。

def build_car_info(manufacturer, model, **other_info):car_info = {}car_info['manufacturer'] = manufacturercar_info['model'] = modelfor key, value in other_info.items():car_info[key] = valuereturn car_infocar = build_car_info('subaru', 'outback', color='blue', tow_package=True)
print(car)

输出:

{'manufacturer': 'subaru', 'model': 'outback', 'tow_package': True, 'color': 'blue'}

 
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)
# 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)

输出:

Printing model: dodecahedron
Printing model: robot pendant
Printing model: iphone caseThe following models have been printed:
dodecahedron
robot pendant
iphone case

Python习题——2018-03-28作业相关推荐

  1. 自学python的书籍逐级推荐-适合初学者和经验的十大最佳Python书籍-2018

    1,最佳Python书籍清单 在这篇文章中,我收集了一些适合初学者和经验丰富的最佳Python书籍的信息.我们还提到了每本书的简要介绍.这将帮助您根据您的要求选择最好的python书籍.此外,它还涵盖 ...

  2. appium的python教程_移动App Appium自动化测试教程Appium+Python 【2018年新】_IT教程网...

    资源名称:移动App Appium自动化测试教程Appium+Python [2018年新] 资源目录: 第一章:App自动化测试概述 1-1 Appium自动化课程简介 1-2 课程大纲 1-3 移 ...

  3. pythonapp自动化_移动App Appium自动化测试教程Appium+Python 【2018年新】

    资源介绍 资源名称:移动App Appium自动化测试教程Appium+Python [2018年新] 资源目录: 第一章:App自动化测试概述 1-1 Appium自动化课程简介 1-2 课程大纲 ...

  4. 2018.03.18 临汾市游记

    2018.03.18 临汾市游记 写在前面 首先,Capella 极其擅长记流水账,包括本文. 其次,本文中所有并列关系的人名,均按字典序排列. 背景 临汾一中 Mr_Wolfram 和 poorpo ...

  5. Python.习题五 列表与元组(下)

    Python.<习题五> 列表与元组 11.假设列表lst_info=[["李玉","男",25],["金忠","男& ...

  6. Linux5.28作业详解磁盘配额与测试

    Linux5.28作业详解 1.分别为自己和本班同学分别创建账号,为本班创建一个用户组,将班上同学加入这个用户组 su root 进入管理员模式 [root@xxx 桌面]# useradd xxx ...

  7. 南开大学python编程基础_[南开大学]20春学期《Python编程基础》在线作业(答案100分)...

    [奥鹏]-[南开大学]20春学期(1709.1803.1809.1903.1909.2003)<Python编程基础>在线作业 试卷总分:100    得分:100 第1题,已知" ...

  8. 【Python习题】餐馆的菜单算账(保姆级图文+实现代码)

    目录 题目(来自) 思路 代码 实现效果 总结 主要内容是校设课程的习题和课外学习的一些习题. 欢迎关注 『Python习题』 系列,持续更新中 欢迎关注 『Python习题』 系列,持续更新中 题目 ...

  9. python – IOError:[Errno 28] pip install 设备上没有空间

    python – IOError:[Errno 28]安装pytorch时设备上没有空间 参考:https://blog.csdn.net/weixin_37340613/article/detail ...

  10. Contest2071 - 湖南多校对抗赛(2015.03.28)

    Contest2071 - 湖南多校对抗赛(2015.03.28) 本次比赛试题由湖南大学ACM校队原创 http://acm.csu.edu.cn/OnlineJudge/contest.php?c ...

最新文章

  1. 也欢迎您访问我的个人主页http://www.april1985.com(原hesicong.com或april1985.com)
  2. Guide: Solr performance tuning--转载
  3. MongoDB(五)-- 副本集(replica Set)
  4. linux音乐关机,在Deepin操作系统中关闭或者更改开机关机音乐的方法
  5. webpack 的使用1
  6. Android中CursorLoader的使用、原理及注意事项
  7. 什么是陀螺仪的dr算法_PID控制器调参工具——DR-PID Tuning(Matlab GUI)
  8. OpenCV2.3.1+VS2005配置方法
  9. li指令 汇编_汇编指令简介
  10. 【原】小软件开发心得(二)——推广、测试
  11. 【问题解决】java.sql.SQLException: null, message from server: “Host ‘xxx.xx.xx.xxx‘ is blocked because of
  12. 奇奇怪怪的大佬:从职业赌徒到互联网大佬
  13. android手机的文件格式,安卓手机如何打开.apk文件?
  14. MATLAB与C++接口(上)(看这一篇足够了!!!)
  15. 【uni-app】App实现二维码分享图合成(支持单张或多张)
  16. hdu2545树上战争
  17. sap月结问题之-ckmlpp物料帐期问题。
  18. linux快速入门 快捷高效学习方法
  19. linux /sys
  20. 只需三步:在CKEditor4富文本编辑器中集成错别字在线检测

热门文章

  1. java 实现点击率_redis实现点击量/浏览量
  2. linux 谷歌浏览器设置代理_Linux系统下Firefox浏览器SSH代理服务器脚本及设置方法...
  3. Spring Value注解的使用
  4. 如何完整删除Windows.old(详细教程)
  5. Could not open client transport with JDBC Uri: jdbc:hive2://slaver2:10000: java.net.ConnectException
  6. 利用百度云存储制作外链mp3音乐地址
  7. 一个low逼的boofuzz脚本生成器
  8. UCenter的百科
  9. [Game Engine]开源游戏框架(转至wiki)
  10. python 源代码 macd双底 高 低_利用Python实现MACD''顶底背离''形态,并实现自动化交易!...