Python核心第二习题答案.docx

上传人:小飞机 文档编号:3164878 上传时间:2023-03-11 格式:DOCX 页数:8 大小:38.74KB
返回 下载 相关 举报
Python核心第二习题答案.docx_第1页
第1页 / 共8页
Python核心第二习题答案.docx_第2页
第2页 / 共8页
Python核心第二习题答案.docx_第3页
第3页 / 共8页
Python核心第二习题答案.docx_第4页
第4页 / 共8页
Python核心第二习题答案.docx_第5页
第5页 / 共8页
亲,该文档总共8页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《Python核心第二习题答案.docx》由会员分享,可在线阅读,更多相关《Python核心第二习题答案.docx(8页珍藏版)》请在三一办公上搜索。

1、Python核心第二习题答案Pytho核心编程第二版答案 第二章 2-3 a=int(input(A=) b=int(input(B=) print(A-B=%d,A+B=%dnA/B=%d,A*B=%d,A%B=%d % (a-b ,a+b ,a/b,a*b,a%b) 2-4 print(input(Enter a string:) print(int(input(Enter a value:) 2-5 i=0 whilei11: print(i) i+=1 fori in range(11): print(i) 2-6 v=int(input(Enter a number to deter

2、mine positive or negative:) if v0: print(%d is negative % v) elif v=0: print(you enter zero) else: print(%d is positive % v) 2-7 while 循环 s=input(Enter a string:) i=0 whileilen(s): print(si) i+=1 for 循环 s=input(Enter a string:) fori in s: print(i) 2-8 while 循环 print(Enter five number) v= i=0 s=0 whi

3、lei5 : a=input(n%d= % (i+1) v.extend(int(a) s=s+vi i+=1 print(v) print(sum=%d % s) for 循环 print(Enter five number) v= s=0 fori in range(5): a=input(n%d= % (i+1) v.extend(int(a) s=s+vi print(v) print(sum=%d % s) 2-9 print(Enter five number) v= s=0 fori in range(5): a=input(n%d= % (i+1) v.extend(int(a

4、) s=s+vi print(v) print(ave=%f % (float(s)/len(v) 2-10 i=1 while i: n=int(input(Enter a number between 1-100:) if n=1: print(Done) i=0 else: print(Error) 2-11 while True: s=0 c=int(input(1sum,2ave,0exit:n) if c=1: fori in range(5): n=int(input(n%d= % (i+1) s+=n print(s) if c=2: fori in range(5): n=i

5、nt(input(n%d= % (i+1) s+=n print(float(s/5) if c=0: break 2-15 a=int(input(a=) b=int(input(b=) c=int(input(c=) n= if ab: a,b=b,a if ac: a,c=c,a if bc: b,c=c,b n.extend(a,b,c) print(n) 第三章; importos ls=os.linesep def write: while True: fname=input(Enter file name:) ifos.path.exists(fname): print(Erro

6、r %s already exists % fname) else: break all= print(nEnter lines (. 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) def read: while True: fname=input(Enter filename:) if no

7、t os.path.exists(fname): print(sorry,%s is not exists % fname) else: break try: fobj=open(fname,r) exceptIOError as e: print(* file open error ,e) else: foreachline in fobj: print (eachline), fobj.close i=0 whilei!=q: i=input(rread,w,write,qquite:) ifi=r: read elifi=w: write 第四,五章 4-1 python对象的三个属性,

8、身份,类型,值 除了值之外其他两个都是只读的 如果对象支持更新操作,那么他的值就是可改变的,否 也是只读的 5-2 defmul(a,b): m=a*b return(m) x=int(input(x=) y=int(input(y=) print(mul(x,y) 5-3 def grade(a): if a100 or 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(Enter your score:) if x

9、=0: break else: grade(x) 5-4 defly(y): if (y%100!=0 and y%4=0) or y%400=0: print(%d is leap year % y) else: print(%d is not leap year % y) while True: x=int(input(Enter the year:) if x=0: break else: ly(x) 5-5 def cd(m0): if m01: print(Error,请输入0到1之间的金额) else: m=m0*100 a=m/25 a1=m%25 b=a1/10 b1=a1%1

10、0 c=b1/5 c1=b1%5 d=c1 print(%f美元=25美分x%d+10美分x%d+5美分x%d+1美分x%d % (m0,a,b,c,d) while True: m0=float(input(请输入金额:) if m0=0: break else: cd(m0) 5-6 while True: s=input(请输入要计算的式子:n) if s=0: break else: ifs.find(*)!=-1: ls=s.split(*) print(float(ls0)*float(ls1) elifs.find(+)!=-1: ls=s.split(+) print(floa

11、t(ls0)+float(ls1) elifs.find()!=-1: ls=s.split() print(float(ls0)*float(ls1) elifs.find(/)!=-1: ls=s.split(/) print(float(ls0)/float(ls1) elifs.find(%)!=-1: ls=s.split(%) print(float(ls0)%float(ls1) 5-8 import math defsqcu: a=float(input(输入正方形或立方体的边长:) print(边长为,a,的正方形面积是,round(a*a,3) print(边长为,a,立方

12、体体积是,a*3) defcisp: r=float(input(输入圆或球的半径:) print(半径为,r,的圆面积是,round(math.pi*(r*2),3) print(半径为,r,的球的体积是,round(math.pi*(r*3)*4/3,3) sqcu cisp 5-10 defFtoC: f=float(input(输入华氏温度:) c=(f-32)*(5/9) print(f,华氏度等于,round(c,3),摄氏温度) FtoC 5-11 defevod: e= o= fori in range(21): if i%2=0: e.append(i) else: o.ap

13、pend(i) print(偶数:,e,n奇数:,o) defjudiv: a,b=int(input(enter the first number:),int(input(enter the second number:) ifa%b=0: print(a,能被,b,整除) else: print(a,不能被,b,整除) evod judiv 5-13 defhmtom: hm=input(输入时间(格式hh:mm):) lhm=hm.split(:) m=float(lhm0)*60+float(lhm1) print(m) hmtom 5-15 defgcd: a,b=int(input

14、(a=),int(input(b=) t1,t2=a,b while b!=0: if a=p: s=s-p n=n+1 print(n, ,p, ,round(s,2) print(n, ,round(s,2), ,p) while True: payment 5-17 import random l= i=0 N=random.randint(2,100) whileiN : n=random.randint(0,2*31) l.append(n) i+=1 print(l) print(n) for x in range(len(l): for y in range(x,len(l): if lxly: ly,lx=lx,ly print(l)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号