欢迎来到天天文库
浏览记录
ID:32192900
大小:55.87 KB
页数:12页
时间:2019-02-01
《python核心第二版习题答案》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Pytho核心编程第二版(第二,三,四,五章)答案第二章2-3 a=int(input('A='))b=int(input('B='))print('A-B=%d,A+B=%dA/B=%d,A**B=%d,A%%B=%d'%(a-b,a+b,a/b,a**b,a%b)) 2-4(a)print(input('Enterastring:'))(b)print(int(input('Enteravalue:')))2-5(a)i=0whilei<11: print(i) i+=1(b)foriinrang
2、e(11): print(i) 2-6v=int(input('Enteranumbertodeterminepositiveornegative:'))ifv<0: print('%disnegative'%v)elifv==0: print('youenterzero')else: print('%dispositive'%v)2-7while循环s=input('Enterastring:')i=0whilei3、'Enterastring:')foriins: print(i)2-8while循环print('Enterfivenumber')v=[]i=0s=0whilei<5: a=input('n%d='%(i+1)) v.extend([int(a)]) s=s+v[i] i+=1print(v)print('sum=%d'%s)for循环print('Enterfivenumber')v=[]s=0foriinrange(5): a=input('n%d='%(i+1)) v.4、extend([int(a)]) s=s+v[i] print(v)print('sum=%d'%s)2--9print('Enterfivenumber')v=[]s=0foriinrange(5): a=input('n%d='%(i+1)) v.extend([int(a)]) s=s+v[i] print(v)print('ave=%f'%(float(s)/len(v)))2-10i=1whilei: n=int(input('Enteranumberbetween1-100:5、')) ifn<=100andn>=1: print('Done') i=0 else: print('Error')2-11whileTrue: s=0 c=int(input("'1'sum,'2'ave,'0'exit:")) ifc==1: foriinrange(5): n=int(input('n%d='%(i+1))) s+=n print(s) if c==2: foriinrange(5): n=int(i6、nput('n%d='%(i+1))) s+=n print(float(s/5)) ifc==0: break2-15a=int(input('a='))b=int(input('b='))c=int(input('c='))n=[]ifa>b: a,b=b,aif a>c: a,c=c,aif b>c: b,c=c,bn.extend([a,b,c])print(n) 第三章;importosls=os.linesepdefw7、rite(): whileTrue: fname=input('Enterfilename:') ifos.path.exists(fname): print('Error%salreadyexists'%fname) else: break all=[] print("Enterlines('.'byitselftoquit).") whileTrue: entry=input('>') ifent8、ry=='.': break else: all.append(entry) fobj=open(fname,'w') fobj.writelines(['%s%s'%(x,ls)forxinall]) fobj.close() print('Done')defread(): whileTrue: fname=inpu
3、'Enterastring:')foriins: print(i)2-8while循环print('Enterfivenumber')v=[]i=0s=0whilei<5: a=input('n%d='%(i+1)) v.extend([int(a)]) s=s+v[i] i+=1print(v)print('sum=%d'%s)for循环print('Enterfivenumber')v=[]s=0foriinrange(5): a=input('n%d='%(i+1)) v.
4、extend([int(a)]) s=s+v[i] print(v)print('sum=%d'%s)2--9print('Enterfivenumber')v=[]s=0foriinrange(5): a=input('n%d='%(i+1)) v.extend([int(a)]) s=s+v[i] print(v)print('ave=%f'%(float(s)/len(v)))2-10i=1whilei: n=int(input('Enteranumberbetween1-100:
5、')) ifn<=100andn>=1: print('Done') i=0 else: print('Error')2-11whileTrue: s=0 c=int(input("'1'sum,'2'ave,'0'exit:")) ifc==1: foriinrange(5): n=int(input('n%d='%(i+1))) s+=n print(s) if c==2: foriinrange(5): n=int(i
6、nput('n%d='%(i+1))) s+=n print(float(s/5)) ifc==0: break2-15a=int(input('a='))b=int(input('b='))c=int(input('c='))n=[]ifa>b: a,b=b,aif a>c: a,c=c,aif b>c: b,c=c,bn.extend([a,b,c])print(n) 第三章;importosls=os.linesepdefw
7、rite(): whileTrue: fname=input('Enterfilename:') ifos.path.exists(fname): print('Error%salreadyexists'%fname) else: break all=[] print("Enterlines('.'byitselftoquit).") whileTrue: entry=input('>') ifent
8、ry=='.': break else: all.append(entry) fobj=open(fname,'w') fobj.writelines(['%s%s'%(x,ls)forxinall]) fobj.close() print('Done')defread(): whileTrue: fname=inpu
此文档下载收益归作者所有