Python核心第二版习题答案
更新时间:2023-04-09 15:39:01 阅读量: 实用文档 文档下载
Python 核心第二版答案第二章
2-4
(a)a=intput(“please input a string”)
print(a)
(b)a=int(intput(“please input a number”))
print(a)
2-5
(a) i=-1
while i<10:
i+=1
print(i)
(c)for I in range(11):
print(i)
2-6
a=int(input("请输入一个数判断正负:"))
if a>0:
print("positive")
else:
if a<0:
print("negative")
else:
print("zero")
2-7
a=input("please enter a string:")
for i in a:
print(i)
2-8
print("please input five numbers:")
a0=int(input())
a1=int(input())
a2=int(input())
a3=int(input())
a4=int(input())
b=[]
b.extend([a0,a1,a2,a3,a4])
sum=0
i=-1
while i<4:
i+=1
sum=sum+b[i]
print(sum)
2-9
a=[89,9,27,19,35]
sum=0
for i in range(5):
sum+=a[i]
ave=float(sum/len(a))
print(ave)
2-10
t=1
while t==1:
a=int(input("请输入1到100之间的任一个数字:"))
if a<1 or a>100:
t=1
else:
print("bingo")
t=0
2-11
t=1
while t==1:
print("请选择操作(1,求五个数之和;2,求五个数的平均数;0,退出程序")
a=int(input())
b=[]
if a==1:
print("请输入五个数字")
a0=int(input())
a1=int(input())
a2=int(input())
a3=int(input())
a4=int(input())
b.extend([a0,a1,a2,a3,a4])
sum=0
for i in range(len(b)):
sum=sum+b[i]
print(sum)
else:
if a==2:
print("请输入五个数字")
a0=int(input())
a1=int(input())
a2=int(input())
a3=int(input())
a4=int(input())
b.extend([a0,a1,a2,a3,a4])
sum=0
for i in range(len(b)):
sum=sum+b[i]
ave=float(sum/len(b))
print(ave)
else:
if a==0:
t=0
2-15
从大到小排列(从小到大排列只需将<改成>)
print("please input three numbers to sort")
a=int(input())
b=int(input())
c=int(input())
d=[]
if a a,b=b,a if a a,c=c,a if b b,c=c,b d.extend([a,b,c]) print(d) 第三章; Maketextfile.py import os ls=os.linesep while True: fname=input("input the file name:") if os.path.exists(fname): print("error:'%s' already exists" % fname) else: break all=[] print("\n enterlines ('.' by itself to quit). \n") while True: entry=input('>') if entry=='.': break else: all.append(entry) fobj=open(fname,'w') fobj.writelines(['%s%s' % (x,ls) for x in all]) fobj.close() print("done") readtext.py fname=input("enter filename:") print() try: fobj=open(fname,'r') except IOError as e: print("o o o file open error:" , e) else: for eachline in fobj: print(eachline) fobj.close() 第五章 5-2 def mul(a,b): m=a*b return m print("请输入两个数求乘积") x=int(input()) y=int(input()) d=mul(x,y) print(d) 5-3 def grade(a): if a>100 or a<0: print("error,please,input your score between 1 to 100") elif a>=90: print("A") elif a>=80: print("B") elif a>=70: print("C") elif a>=60: print("D") else: print("F") while True: x=int(input("please, input your score:")) if x==0: break else: grade(x) 5-4 def ly(y): if y%4==0 and y%100!=0: print("%d is leap year" % y) else: print("%d isn't leap year" % y) while True: x=int(input('please enter a year:')) if x==0: break else: ly(x) 5-5 def cd(a): b=a*100 a1=int(b/25) a2=int((b-25*a1)/10) a3=int((b-25*a1-10*a2)/5) a4=int(b-25*a1-10*a2-a3*5) print("%f可以兑换%d张25美分,%d张10美分,%d张5美分,%d张1美分" % (a,a1,a2,a3,a4)) x=a=float(input('请输入你要换算的金额(小于一美元):')) cd(x) 5-8 import math def sqcube(): s = float(input('enter length of one side: ')) print ('the area is:', s ** 2., '(units squared)') print ('the volume is:', s ** 3., '(cubic units)') def cirsph(): r = float(input('enter length of radius: ')) print ('the area is:', math.pi * (r ** 2.),'(units squared)') print ('the volume is:', (4. / 3.) * math.pi * (r **3.), '(cubic units)') sqcube() cirsph() 5-10 def tem(): f=float(input('请输入华氏温度:')) t=(f-32)*(5/9) print('%f华氏度=%f摄氏度' % (f,t)) tem() 5-11 (a) for i in range(21): if i%2==0: print(i) (b for i in range(21): if i%2==1: print(i) (c)) 能否被2整除 d) a=int(input()) b=int(input()) if a a,b=b,a if a%b==0: print('true') else: print('false') 5-13 def mini(a, b): return a * 60 + b time =input('Please input the time use HH:MM format: ') t = time.split(':') print (mini(int(t[0]), int(t[1]))) 5-14 def rate(r): m=(1+r)**365-1 return m x=float(input()) print(rate(x)) 5-15 def gcd_lcm(): print('请输入两个整数:') a=int(input()) b=int(input()) t1=a t2=b while b!=0: if a a,b=b,a else: a,b=b,a%b print('最大公约数是:%d最大公倍数是:%d' % (a,t1*t2/a)) gcd_lcm()
正在阅读:
Python核心第二版习题答案04-09
洞门后浇环梁技术交底 - 图文11-30
最熟悉的人作文07-05
隐蔽工程验收记录填写09-01
糖果传奇iOS和安卓数据互通不?08-07
高速公路施工质量通病防治手册(路基 桥梁 隧道) - secret06-04
2010EXCEL试题01-22
高职院校与企业共同培养高技能人才优势研究08-30
PIM组播协议稀疏模式SM模式 - 图文03-09
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- 习题
- 核心
- 答案
- Python
- 2081 DTⅡ型带式输送机设计(含全套毕业说明书和机械CAD图纸)
- 可长时粘贴便利贴的制作技术
- 2022-2022年高中物理沪科版《必修二》《第一章 怎样研究抛体运动
- Chapter 8 Trellis and graph based codes基于网格和图形的编码
- 高考数学微专题---外接球(教师版)
- 2022年江西财经大学宏观经济学复试仿真模拟三套题
- 《情绪管理、压力舒解与中医养生》
- 2022年厉行节约反对浪费个人心得体会.doc
- 全国高等教育自学考试《现代管理学》 课程代码0107 考纲详解 (全
- 最新语文版初中语文八年级下册《邹忌讽齐王纳谏》教学设计精编版
- 2016年广东中学综合实力排名榜广东实验中学居然排…
- 2022年南京大学生命科学学院858细胞生物学二考研基础五套测试题
- 保育员工作心得体会
- (全国通用)2022中考历史总复习第一部分中国古代史第四单元统一多
- 英语演讲话题热点精选50个资料
- 作文范文之6年级英语作文100字有中文
- 2008-2014年江苏高中学业水平测试(生物试卷和答案)
- 风水知识 屋里放一物能辟邪驱灾招财
- 江西理工大学大学数值分析(计算方法)期末试卷3及参考答案
- 液压系统泄漏的解决方法