欢迎来到天天文库
浏览记录
ID:50455113
大小:184.12 KB
页数:15页
时间:2020-03-06
《python核心编程答案.pdf》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、python核心练习(第二章)2-3a=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)foriinrange(11):print(i)2-6v=int(in
2、put('Enteranumbertodeterminepositiveornegative:'))ifv<0:print('%disnegative'%v)elifv==0:print('youenterzero')else:print('%dispositive'%v)2-7while循环s=input('Enterastring:')i=0whilei3、('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.extend([int(a)])s=s+v[i]print(v)print('sum=%d'%s)2--9print('Enterfivenumber4、')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,'5、0'exit:"))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=[]ifa>b:a,b=b,aifa>c:a,c=c,aifb>c:b,c=c,bn.extend(6、[a,b,c])print(n)python核心练习(第三章)importosls=os.linesepdefwrite():whileTrue:fname=input('Enterfilename:')ifos.path.exists(fname):print('Error%salreadyexists'%fname)else:breakall=[]print("Enterlines('.'byitselftoquit).")whileTrue:entry=input('>')ifentry=='.':brea7、kelse: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')exceptIOEr8、rorase:print("***fileopenerror",e)else:foreachlineinfobj:print(eachline),fobj.close()i=0whilei!='q':i=input("'r'read,'w',write,'q'quite:")ifi=='r':read()elifi=
3、('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.extend([int(a)])s=s+v[i]print(v)print('sum=%d'%s)2--9print('Enterfivenumber
4、')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,'
5、0'exit:"))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=[]ifa>b:a,b=b,aifa>c:a,c=c,aifb>c:b,c=c,bn.extend(
6、[a,b,c])print(n)python核心练习(第三章)importosls=os.linesepdefwrite():whileTrue:fname=input('Enterfilename:')ifos.path.exists(fname):print('Error%salreadyexists'%fname)else:breakall=[]print("Enterlines('.'byitselftoquit).")whileTrue:entry=input('>')ifentry=='.':brea
7、kelse: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')exceptIOEr
8、rorase:print("***fileopenerror",e)else:foreachlineinfobj:print(eachline),fobj.close()i=0whilei!='q':i=input("'r'read,'w',write,'q'quite:")ifi=='r':read()elifi=
此文档下载收益归作者所有