Pytho核心编程第二版(第二,三,四,五章)答案第二章2-3a=int(input('A='))b=int(input('B='))print('A-B=%d,A+B=%d\nA/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=0whilei11:print(i)i+=1(b)foriinrange(11):print(i)2-6v=int(input('Enteranumbertodeterminepositiveornegative:'))ifv0:print('%disnegative'%v)elifv==0:print('youenterzero')else:print('%dispositive'%v)2-7while循环s=input('Enterastring:')i=0whileilen(s):print(s[i])i+=1for循环s=input('Enterastring:')foriins:print(i)2-8while循环print('Enterfivenumber')v=[]i=0s=0whilei5: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.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:'))ifn=100andn=1:print('Done')i=0else:print('Error')2-11whileTrue:s=0c=int(input('1'sum,'2'ave,'0'exit:\n))ifc==1:foriinrange(5):n=int(input('n%d='%(i+1)))s+=nprint(s)ifc==2:foriinrange(5):n=int(input('n%d='%(i+1)))s+=nprint(float(s/5))ifc==0:break2-15a=int(input('a='))b=int(input('b='))c=int(input('c='))n=[]ifab:a,b=b,aifac:a,c=c,aifbc:b,c=c,bn.extend([a,b,c])print(n)第三章;importosls=os.linesepdefwrite():whileTrue:fname=input('Enterfilename:')ifos.path.exists(fname):print('Error%salreadyexists'%fname)else:breakall=[]print(\nEnterlines('.'byitselftoquit).\n)whileTrue:entry=input('')ifentry=='.':breakelse:all.append(entry)fobj=open(fname,'w')fobj.writelines(['%s%s'%(x,ls)forxinall])fobj.close()print('Done')defread():whileTrue:fname=input('Enterfilename:')ifnotos.path.exists(fname):print('sorry,%sisnotexists'%fname)else:breaktry:fobj=open(fname,'r')exceptIOErrorase:print(***fileopenerror,e)else:foreachlineinfobj:print(eachline),fobj.close()i=0whilei!='q':i=input('r'read,'w',write,'q'quite:)ifi=='r':read()elifi=='w':write()第四,五章4-1python对象的三个属性,身份,类型,值(ID,type,value)除了值之外其他两个都是只读的如果对象支持更新操作,那么他的值就是可改变的,否也是只读的5-2defmul(a,b):m=a*breturn(m)x=int(input('x='))y=int(input('y='))print(mul(x,y))5-3defgrade(a):ifa100ora0:print(error,please,inputyourscorebetween1to100)elifa=90:print(A)elifa=80:print(B)elifa=70:print(C)elifa=60:print(D)else:print(F)whileTrue:x=int(input(Enteryourscore:))ifx==0:breakelse:grade(x)5-4defly(y):if(y%100!=0andy%4==0)ory%400==0:print('%disleapyear'%y)else:print('%disnotleapyear'%y)whileTrue:x=int(input('Entertheyear:'))ifx==0:breakelse:ly(x)5-5defcd(m0):ifm00orm01:print('Error,请输入0到1之间的金额')else:m=m0*100a=m/25a1=m%25b=a1/10b1=a1%10c=b1/5c1=b1%5d=c1print('%f美元=25美分x%d+10美分x%d+5美分x%d+1美分x%d'%(m0,a,b,c,d))whileTrue:m0=float(input('请输入金额:'))ifm0==0:breakelse:cd(m0)5-6whileTrue:s=input('请输入要计算的式子:\n')ifs==0:breakelse:ifs.find('*')!=-1:ls=s.split('*')print(float(ls[0])*float(ls[1]))elifs.find('+')!=-1:ls=s.split('+')print(float(ls[0])+float(ls[1]))elifs.find(^)!=-1:ls=s.split(^)print(float(ls[0])**float(ls[1]))elifs.find('/')!=-1:ls=s.split('/')print(float(ls[0])/float(ls[1]))elifs.find('%')!=-1:ls=s.split('%')print(float(ls[0])%float(ls[1]))5-8importmathdefsqcu():a=float(input('输入正方形或立方体的边长:'))print('边长为',a,'的正方形面积是',round(a*a,3))print('边长为',a,'立方体体积是',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-10defFtoC():f=float(input('输入华氏温度:'))c=(f-32)*(5/9)print(f,'华氏度等于',round(c,3),'摄氏温度')FtoC()5-11defevod():e=[]o=[]foriinrange(21):ifi%2==0:e.append(i)else:o.append(i)print('偶数:',e,'\n奇数:',o)defjudiv():a,b=int(input('enterthefirstnumber:')),int(input('enterthesecondnumber:'))ifa%b==0:print(a,'能被',b,'整除')else:print(a,'不能被',b,'整除')evod()judiv()5-13defhmtom():hm=input(输入时间(格式'hh:mm'):)lhm=hm.split(':')m=float(lhm[0])*60+float(lhm[1])print(m)hmtom()5-15defgcd():a,b=int(input('a=')),int(input('b='))t1,t2=a,bwhileb!=0:ifab:a,b=b,aelse:a,b=b,a%bprint('最大公约数是%d,最小公倍数是%d'%(a,t1*t2/a))gcd()5-16defpayment():s=float(input('Enteropeningbalance:'))p=float(input('Entermonthlypayment:'))print(Pymt#PaidRemainingBalance)print(-------------------------)n=0print(n,,p,,round(s,2))whiles=p:s=s-pn=n+1print(n,,p,,round(s,2))print(n,,round(s,2),,p)whileTrue:payment()5-17importrandoml=[]i=0N=random.randint(2,100)whileiN:n=random.randint(0,2**31)l.append(n)i+=1print(l)print('\n')forxinrange(len(l)):foryinrange(x,len(l)):ifl[x]l[y]:l[y],l[x]=l[x],l[y]print(l)