python for everybody作业和测试答案_PY4E-Python for Everybody课后作业答案
本文练习题答案仅涉及编程部分,其余部分略过。每一道题都经过运行确保正确,仅供参考!
Chapter 1 为什么要学编程?
较简单、概念化。略
Chapter 2 变量、表达式、语句
习题 2: 使⽤ input 编写⼀个程序,提⽰⽤⼾输⼊姓名,然后打印欢迎语。
name = input("Enter your name:\n")
print("Hello " + name)
习题 3: 编写⼀个程序,提⽰⽤⼾输⼊⼯时和时薪,然后计算出总⼯资。
Hours = input("Enter Hours:\n")
Rate = input("Enter Rate:\n")
Pay = float(Hours) * float(Rate)
print("Pay:\n", Pay)
练习 5: 写⼀个程序,提⽰⽤⼾输⼊摄⽒温度,然后将其转化成华⽒温度,并且把结果打印出来。
a = float(input('请输入摄氏温度:'))
b = a * 9 / 5 + 32
print("摄氏温度{}转换为华氏温度为:{}".format(a, b))
Chapter 3 条件执行
Exercise 1: Rewrite your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours.(练习1:重写您的薪资计算,以使雇员工作时间超过40小时以上的小时工资为1.5倍。)
#练习1
prompt1 = input('Enter Hours:')
Hours = int(prompt1)
prompt2 = input('Enter Rate:')
Rate = int(prompt2)
if 0 <= Hours <= 40:
Pay = Hours * Rate
print(Pay)
if Hours > 40:
Pay = 40 * Rate +(Hours-40)*(1.5 * Rate)
print(Pay)
Exercise 2: Rewrite your pay program using try and except so that your program handles non-numeric input gracefully by printing a message and exiting the program. The following shows two executions of the program: (练习2:使用try和except重写您的付款程序,以便您的程序通过打印一条消息并退出该程序来正常处理非数字输入。以下显示了该程序的两个执行:)
#练习2
prompt1 = input('Enter Hours:')
prompt2 = input('Enter Rate:')
try:
Hours = int(prompt1)
Rate = int(prompt2)
if 0 <= Hours <= 40:
Pay = Hours * Rate
print(Pay)
if Hours > 40:
Pay = 40 * Rate +(Hours-40)*(1.5 * Rate)
print(Pay)
except:
print("Error, please enter numeric input")
Exercise 3: Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error message. If the score is between 0.0 and 1.0, print a grade using the following table: (练习3:编写程序以提示得分在0.0到1.0之间。如果分数超出范围,则打印错误消息。如果分数在0.0到1.0之间,请使用下表打印成绩:)
prompt = input('Enter score:')
try:
score = float(prompt)
if 0 <= score <= 1:
if score >= 0.9:
print(score, " A")
elif score >= 0.8:
print(score, " B")
elif score >= 0.7:
print(score, " C")
elif score >= 0.6:
print(score, " D")
else:
print(score, " F")
else:
print("Please enter a number between [0,1]")
except:
print("Please enter a number")
Chapter 4 Functions
Exercise 6: Rewrite your pay computation with time-and-a-half for overtime and create a function called computepay which takes two parameters (hours and rate). (练习6:用半小时的超时时间来重写您的工资计算,并创建一个称为computepay的函数,该函数需要两个参数(小时数和费率)。)
prompt1 = input('Enter Hours:')
prompt2 = input('Enter Rate:')
def computepay(Hours, Rate):
try:
Hours = int(prompt1)
Rate = int(prompt2)
if 0 <= Hours <= 40:
Pay = Hours * Rate
print(Pay)
if Hours > 40:
Pay = 40 * Rate + (Hours - 40) * (1.5 * Rate)
print(Pay)
except:
print("Error, please enter numeric input")
computepay(prompt1,prompt2)
Exercise 7: Rewrite the grade program from the previous chapter using a function called computegrade that takes a score as its parameter and returns a grade as a string. (练习7:使用称为computegrade的函数重写上一章的成绩程序,该函数将分数作为参数并以字符串形式返回成绩。)
#练习7
prompt = input('Enter score:')
def computegrade(score):
try:
score = float(prompt)
if 0 <= score <= 1:
if score >= 0.9:
print(score, " A")
elif score >= 0.8:
print(score, " B")
elif score >= 0.7:
print(score, " C")
elif score >= 0.6:
print(score, " D")
else:
print(score, " F")
else:
print("Please enter a number between [0,1]")
except:
print("Please enter a number")
computegrade(prompt)
Chapter 5 Iteration
Exercise 1: Write a program which repeatedly reads numbers until the user enters "done". Once "done"is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number.(练习1:编写一个程序,该程序反复读取数字,直到用户输入“done”为止。输入“done”后,打印出总数,计数和平均值。如果用户输入的不是数字,则使用try和except检测到他们的错误,并打印错误消息并跳至下一个数字。)
答案1:
#该代码中没有使用try和except检查错误,在检验输入是否为数字的时候用到了isdigit()函数。
count = 0
total = 0
while True:
shuru = input('Enter a number: ')
if shuru.isdigit():
count = count + 1
total = total + int(shuru)
continue
if shuru == 'done':
break
else:
print("Invalid input")
print('Done!')
print("num: ", count)
print("sum: ", total)
ave = total / count
print("ave: ", ave)
答案2:
#改代码中用了try和except检查错误,但是判断输入的是否为数字时使用了float(),不知道还有没有更好地判断方法
count = 0
total = 0
while True:
shuru = input('Enter a number: ')
if shuru == 'done':
break
try:
shuru = float(shuru)
count = count + 1
total = total + int(shuru)
continue
except:
print("Invalid input")
print('Done!')
print("num: ", count)
print("sum: ", total)
ave = total / count
print("ave: ", ave)
Exercise 2: Write another program that prompts for a list of numbers as above and at the end prints out both the maximum and minimum of the numbers instead of the average.(练习2:编写另一个程序,提示上述数字列表,最后输出最大和最小数字,而不是平均值。)
#练习2
largest = None
smallest = None
while True:
shuru=input('Enter a number:')
if shuru == "done":
break
try:
shuru = float(shuru)
except:
print("Invalid input")
continue
if largest is None or shuru >= largest:
largest = shuru
if smallest is None or shuru <= smallest:
smallest = shuru
print("largest : ", largest)
print("smallest: ",smallest)
python for everybody作业和测试答案_PY4E-Python for Everybody课后作业答案相关推荐
- [转载] Python语言程序设计基础(第二版)嵩天等课后习题答案
参考链接: Python程序加两个数字 第一次博文 Time:2018年04月28日 星期六 11:37 二次补充 2018年05月02日 第一章 程序设计基本方法 P29 # 1.1字符串的拼接 ...
- 如何用python实现地图定位_GPS 测试汇总和python GPS 导航地图实现
作为GPS 测试专业户出身,一直有想法将GPS数据本地网页化,从而实现动态展示导航数据.在摸索过程中也遇到了问题,因此分享这个文章. 刚毕业的头两年,从事软件测试GPS相关,其间参与多个项目,也和高通 ...
- python程序设计课后答案第二版_Python程序设计课后习题答案-第一单元
Python程序设计课后习题答案-第一单元 习题1 一.选择题 1.Python语言属于().C A.机器语言 B.汇编语言 C.高级语言 D.科学计算语言2.下列选项中,不属于Python特点的是( ...
- python语言的软件包_Softcar测试软件包与Python语言的集成
Softcar 测试软件包与 Python 语言的集成 邹轩 ; 黄义萍 [期刊名称] <微计算机信息> [年 ( 卷 ), 期] 2006(022)029 [摘要] Softcar 和 ...
- 《Python语言程序设计》王恺 机械工业出版社 第二章课后习题答案
第二章 Python的基础语法 2.7 课后习题 (1)变量是指在程序运行讨程中值可以发生改变的量 (2)已知s="Python语言程序设计",则print(s[2:4])的输出结 ...
- c语言课后题2.52.8答案,新概念第二册课后题答案详解:Lesson52
新概念英语作为一套世界闻名的英语教程,以其全新的教学理念,有趣的课文内容和全面的技能训练,深受广大英语学习者的欢迎和喜爱.为了方便同学们的学习,新东方在线新概念英语网为大家整理了最全面的新概念第二册课 ...
- 大学计算机西安电子科技大学答案,计算机网络技术与应用课后题答案(西安电子科技大学)...
计算机网络技术与应用课后题答案(西安电子科技大学) <计算机网络应用基础>试题(1) 一.填空题(每空1分,共24分,答案写在横线上) 1.按逻辑组成划分,计算机网络是由和两部分组成的. ...
- 我要大学答案———小程序开发指南 课后习题答案|实验报告|考研资料|期末真题的答案
我要大学答案-小程序开发原理 大学所有答案都能免费找 设计思路 我要大学答案-小程序 大学所有答案都能免费找 期末考试题库.清华.南昌理工大学.北大.江西理工大学考试题库 期末考试题库.清华.南昌理工 ...
- 微型计算机周明德课后答案,微机原理(周明德)课后题答案..doc
微机原理(周明德)课后题答案. 第1章 作 业 答 案 1.1 微处理器.微型计算机和微型计算机系统三者之间有什么不同? 解: 把CPU(运算器和控制器)用大规模集成电路技术做在一个芯片上,即为微 处 ...
- 河南理工大学c语言程序第六章答案,河南理工大学C语言课后习题答案精解第六章..ppt...
河南理工大学C语言课后习题答案精解第六章. 选择题 (1) C语言中一维数组的定义方式为:类型说明符 数组名 A. [整型常量]B. [整型表达式] C. [整型常量]或[整型常量表达式]D. [变量 ...
最新文章
- GPU对决TPU,英伟达能否守住领先地位?
- Linux Restart PHP
- 【转】关于Azure存储账户
- apache-commons 常用工具类
- vecm模型怎么写系数_时变秩和时变系数VECM模型与“费雪效应”机制检验
- 博图/博途(TIA)V13 V14 V15 V16 软件安装教程,适用于新手的傻瓜式安装方法,强推!!!!
- html武侠文字游戏源码,执剑行!最新武侠文字mud游戏
- shopex手机版之触屏版,html5版,ShopEx手机触屏版V3.0重磅发布!优化用户界面,增强用户体验...
- windows如何安装SVN
- 《Head First设计模式》中文版 读书笔记
- shineblink MQ-3酒精浓度探测
- easyRL蘑菇书阅读笔记(一)
- Linux下的内核线程threaded irq机制分析与应用
- 联想拯救者R720加装固态硬盘过程中遇到的小问题
- Java中==与equals
- MAC查看SVN版本日志
- 小程序未来发展趋势怎样?2020最新趋势分析
- 栈+模拟(大鱼吃小鱼)
- 1-03 C++起步: 用函数组织语句 —— 代码封装初步
- 如何在虚拟机(linux)下运行java程序
热门文章
- google earth pro 安装后启动时报错:google 地球无法连接到登录服务器(错误代码:c000000c)
- 小米笔记本pro充电测试软件,一款给人心理落差较大的笔记本--小米笔记本Pro测评...
- 电子海图制作中坐标转换的应用与实现
- 自考计算机基础知识考题,自考计算机应用基础试题及参考答案
- 单片机原理与应用技术
- python生成3d人体模型_make human开源3D人体建模软件免费下载|make human开源3D人体建模软件2018最新版下载_v1.0.2_9号软件下载...
- 中国管理实践的大趋势
- python保存h264格式视频(linux和windows)
- 循环制比赛要赢几场可能(一定)晋级
- Cisco Packet Tracer 思科模拟器中路由器的DHCP配置