Python核心编程第二版第五章数字(课后习题)----我的答案
5-1.整型。讲讲Python普通整型和长整型的区别。
标准整型类型是最通用最基本的数字类型等价于C语言的长整型,一般以十进制表示。
长整型则是标准整型类型的超集,当需要用到比标准整型类型更大的整型时,长整型就大有作为了。在一个整型后面加上L(l也是可行的),表示整型为长整型。
5-2.操作符。
(a).写一个函数,计算并返回两个数的乘积。
def Multipliers(a, b):return a * b
(b).写一段代码调用这个函数,并显示它的结果。
def Multipliers(a, b):return a * bresult = Multipliers(2, 3)
print("The result is %d" % result)
5-3.标准类型操作符。写一段脚本,输入一个测验成绩,根据下面的标准,输出他的评分成绩(A-F).
A:90~100
B:80~89
C:70~79
D:60~69
F:<60
def testScore():score = int(raw_input("Input your real grade: "))if score >= 90 and score <= 100:print("Your testscore is A.")elif score >= 80 and score <= 89:print("Your testscore is B.")elif score >= 70 and score <= 79:print("Your testscore is C.")elif score >=60 and score <= 69:print("Your testscore is D.")elif score < 60:print("Your failed in this test and got a F.")else:print("Try input some numbers.")testScore()
5-4.取余。判断给定年份是否是闰年。使用下面的公式。
def testYear():years = int(raw_input("Input a year in your mind: "))if (years % 4 == 0 and years % 100 != 0) or (years % 4 == 0 and years % 100 == 0):print("This year is a leap year.")else:print("This is a normal year.")testYear()
5-5.取余。取一个任意小于1美元的金额,然后计算可以换成最少多少枚硬币。
def usDollar():dollars = float(raw_input("Input under 1 dollar like 0.76: "))dollars *= 100quartcoin = dollars // 25tencoin = (dollars - quartcoin * 25) // 10fivecoin = (dollars - quartcoin * 25 - tencoin * 10) // 5onecoin = (dollars - quartcoin * 25 - tencoin * 10 - fivecoin * 5)print("Dollars is %d Quartcoin and %d Tencoin and %d Fivecoin and %d Onecoin."% (quartcoin, tencoin, fivecoin, onecoin))usDollar()
5-6.算术。写一个计算器程序。你的代码可以接受这样的表达式,两个操作数加一个操作符:N1操作符N2。
def Calculator():N1 = int(raw_input("Input number 1: "))N2 = float(raw_input("Input number 2: "))sign = raw_input("Input what you want N1 N2 do: ")if sign == '+':print("N1 + N2 == %f" % (N1 + N2))elif sign == '-':print("N1 - N2 == %f" % (N1 - N2))elif sign == '*':print("N1 * N2 == %f" % (N1 * N2))elif sign == '/':print("N1 / N2 == %f" % (N1 / N2))elif sign == '%':print(N1 % N2)elif sign == '**':print("N1 ** N2 == %f" % (N1 ** N2))else:print("Out of ranges.")Calculator()
5-7.营业税。随意取一个商品金额,然后根据当地营业税额度计算应该交纳的营业税。
假设税率为5%
def taxes(m):percent = 0.05tax = earnmoney * percentreturn tax
if __name__ == '__main__':earnmoney = int(raw_input("Input your earn money number: "))print taxes(earnmoney)
5-8.几何。计算面积和体积。
(a).正方形和立方体
def geo():side = int(raw_input("Input one side with figure: "))print("[s] means square, [c] means cubic")figures = raw_input("input figure you want to calculate: ")if figures == 's':a = side * sideprint("The area is %d. " % a)elif figures == 'c':a = side * side * 6b = side * side * sideprint("The area is %d. The bulk is %d." % (a ,b))geo()
(b).圆和球
def geo():side = float(raw_input("Input one side with figure: "))print("[s] means square, [c] means cubic, [r] means round, [sp] means spheres.")figures = raw_input("input figure you want to calculate: ")if figures == 's':a = side * sideprint("The area is %d. " % a)elif figures == 'c':a = side * side * 6b = side * side * sideprint("The area is %d. The bulk is %d." % (a, b))elif figures == 'sp':a = 4 * 3.14 * side * sideb = 4 / 3 * 3.14 * side * side * sideprint("The area is %f. The bulk is %f." % (a, b))elif figures == 'r':a = 3.14 * side * sideprint("The area is %f." % a)else:print("Please follow the orders.")
geo()
转:
# -*- coding: utf-8 -*-
import math if __name__=='__main__': ss = input() print '正方形面积:',ss*ss print '立方体面积:',ss*ss*6 print '立方体体积:',ss*ss*ss print '圆面积:',ss*ss*math.pi print '球面积:',ss*ss*math.pi*4 print '球体积:',ss*ss*ss*math.pi*4/3.0
5-9.(a).
>>> 17 + 32
49
>>> 017 + 32
47
>>> 017 + 032
41
因为在整型的数值前加上0表示这个数是一个八进制的数。所以017换算成十进制为15,032转换成十进制为26,与下面两个计算结果符合。在十进制数前加0x表示十六进制数。
(b).使用长整型运算更高效。
5-10.转换。写一对函数来进行华氏度到摄氏度的转换。
from __future__ import division
def transForm():print("[f] means Fah, [c] means Cel.")Fah = float(raw_input("Input Fahrenheit: "))Cel = float(raw_input("Input Celsius: "))choice = raw_input(">>> ")if choice == 'f':Cel = (Fah - 32) * (5 / 9)print(Cel)elif choice == 'c':Fah = (Cel * (9 / 5)) + 32print(Fah)else:print("Please follow orders.")transForm()
5-11.取余。
(a).使用循环和算术运算,求出0~20之间的所有偶数。
def reMainder():for i in range(21):if i % 2 == 0:print("The dual is %d" % i)reMainder()
(b).
def reMainder():for i in range(21):if i % 2 == 1:print("The dual is %d" % i)reMainder()
(c).对该数除2取余数,为0就是偶数,为1就是奇数。
(d).
def reMainder():inti1 = int(raw_input("Input an int number: "))inti2 = int(raw_input("Input an other number: "))if inti1 % inti2 == 0:return True
else:return False
print(reMainder())
5-12.系统限制。写一段脚本确认一下你的Python所能处理的整型,长整型,浮点型和复数的范围。
Pass
5-13.转换。写一个函数把由小时和分钟表示的时间转换为只用分钟表示的时间。
def tranS(h, m):mins = hour * 60 + minreturn minshour = int(raw_input("Input hours: "))
min = int(raw_input("Input minutes: "))
if (hour < 23 and hour > -1) and (min < 60 and min > -1):print("The times is %d." % tranS(hour, min))
else:print("Input some right numbers.")
5-14. 银行利息。写一个函数,以定期存款利率为参数, 假定该账户每日计算复利,请计算并返回年回报率。
def inTerest(i, p):return p * (1 + i) ** 365i = float(raw_input("Input rates: "))
p = int(raw_input("Input your money: "))
print("Your interest is %f." % inTerest(i, p))
5-15.最大公约数和最小公倍数。请计算两个整型的最大公约数和最小公倍数。
转
# -*- coding: utf-8 -*- def gongyueshu(m , n): if m< n: min = m else: min = n for i in range(min , 0 ,-1): if m % i ==0 and n % i ==0: return i return 0 def gongbeishu(m , n): l = gongyueshu(m,n) return m * n / l if __name__ == '__main__': m = input() n = input() print '最大公约数:',gongyueshu(m, n) print '最小公倍数:',gongbeishu(m, n)
5-16.转
# -*- coding: utf-8 -*- def Payment(cost,total): count = 0 print ' Amount Remaining' print 'Pymt# Paid Balance' print '----- ------ --------' while True: print '%-2d $%.2f $%6.2f'%(count,total,cost) if cost - total >=0: cost = cost-total else: if cost !=0: print '%-2d $%.2f $%6.2f'%(count+1,cost,0) break count += 1 if __name__=='__main__': cost = input('Enter opening balance:') total = input('Enter monthly payment:') Payment(cost,total)
Python核心编程第二版第五章数字(课后习题)----我的答案相关推荐
- python语言程序设计基础第二版第七章答案-Python核心编程第二版 第七章课后答案...
注:我使用的python3.5. 其中range和xrange,input和raw_input已经合并 7-1.字典方法.哪个字典方法可以用来把两个字典合并到一起. dict.update(dict2 ...
- Python核心编程第二版 第十三章课后答案
13-1.程序设计.请列举一些面对对象编程与传统旧的程序设计形式相比的先进之处. 没什么只有OO能做到,OO更多的是给了你一种能力,一种忽略细节的能力:忽略的越多,人类有限的智力就可以容纳越多越复杂的 ...
- python核心教程第二版答案_python核心编程第二版第4章习题答案.docx
python核心编程第二版第4章习题答案.docx 4-1.Python 对象.与所有 Python 对象有关的三个属性是什么?请简单的描述一下. 答案: 所有的 Python 对象都拥有三个特性:身 ...
- 拒绝从入门到放弃_《Python 核心编程 (第二版)》必读目录
目录 目录 关于这本书 必看知识点 最后 关于这本书 <Python 核心编程 (第二版)>是一本 Python 编程的入门书,分为 Python 核心(其实并不核心,应该叫基础) 和 高 ...
- python核心编程第二版pdf_Python Book电子书pdf版合集 Python核心高级编程第二版
1小时学会Python.doc 51CTO下载-[Python系列].BeginningPythonFromNovicetoProfessionalSecondEdition.pdf 8.Python ...
- python核心编程第二版第一章学习笔记
一.名字来源 贵铎·范·罗萨姆(Guido van Rossum)于1989 年底始创了Python,那时,他还在荷兰的CWI(Centrum voor Wiskunde en Informatica ...
- 《python核心编程第二版》第5章习题
5-1 整形 讲讲 Python 普通整型和长整型的区别 答:普通整型 32位,长整数类型能表达的 数值仅仅与你的机器支持的(虚拟)内存大小有关 5-2 运算符 (a) 写一个函数,计算并返回两个数的 ...
- 《python核心编程第二版》第7章习题
7–1. 字典方法.哪个字典方法可以用来把两个字典合并到一起? 答:dict1.update(dict2) 7–2. 字典的键.我们知道字典的值可以是任意的Python 对象,那字典的键又如何呢?请试 ...
- python嵩天第二版第五章_如何避免从入门到放弃——python小组学习复盘
2019年春节python学习行动复盘2019-02-09 为了主攻python,没有参加心理学晨读.对心理学也不敢兴趣,怕耽误学习python的时间. 那么没学习心理学的情况下,python学的怎么 ...
最新文章
- 牛顿-拉夫逊法 原理讲解以及python算例实现
- angular 字符串转换成数字_Python | 一文看懂Python列表、元组和字符串操作
- scrapy获取a标签的连接_python爬虫——基于scrapy框架爬取网易新闻内容
- 大数据下的质量体系建设
- 深入浅出时序逻辑电路(1)
- oci连接mysql_使用 OCILIB 连接并操作 Oracle 数据库
- 巴菲特将退休并把公司交给网红接管?被一封信恶搞...
- 阿里巴巴开源离线同步工具 DataX3.0 介绍
- 【Faster RCNN】损失函数理解
- 淘宝CPC、CPM和CPS分别是什么,有什么区别?
- excel输入身份证号码变指数 及自动变数值如何解决?
- 安装Windows系统时进行磁盘格式化及分区等操作
- 计算机入门新人必学,电脑新手入门教程 让你快速上手
- 什么是手机证书和签名干什么用的
- 提高 MacBook 电池寿命的 9 种方法
- 微信小程序-B站:wxml和wxss文件
- 转:使用Python对音频进行频谱分析
- oracle建表备份数据,oracle建表备份脚本,如果update的数据不对,可以从WEB_RI_PLYEDR_CED_BAK找回...
- 12亿次月访问流量网站服务器架构探秘
- 2011计算机考研大,2011年计算机考研大纲