登录后查看本帖详细内容!
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
5.1: number=eval(input('enter an integer,the input ends if it is 0:')) if number==0: print('you didn\'t enter any number') else: sum1=0 sum2=0 count1=0 count2=0 while number!=0: if number > 0: sum1+=number count1+=1 number=eval(input('enter an integer,the input ends if it is 0:')) else: sum2+=number count2+=1 number=eval(input('enter an integer,the input ends if it is 0:')) total=sum1+sum2 average=total/(count1+count2) print('the total is',total) print('the average is ',average) 5.2: import random import time count=0 right_count=0 starttime=time.time() while count < 5: number1=random.randint(1,15) number2=random.randint(1,15) answer=eval(input('what is '+str(number1)+'+'+str(number2)+'?')) if answer==number1+number2: right_count+=1 print('right!') else: print('wrong!') count+=1 endtime=time.time() print('you are right '+str(right_count)+'in'+str(count)+'times','you spend '+str(int(endtime-starttime))+'s') 5.3: print('-----------') print(format('公斤','^3s')+'|'+format('磅','^3s')) for i in range(1,20,2): print('-'*5+'|'+'-'*5) print(format(i,'^5d')+'|'+format(i*2.2,'^.1f')) 5.4: print('-----------') print(format('英里','^3s')+'|'+format('公里','^5s')) for i in range(1,11): print('-'*5+'|'+'-'*5) print(format(i,'^5d')+'|'+format(i*1.609,'^.3f')) 5.5: print('━'*25) print(format('公斤','^3s')+'|'+format('磅','^5s')+'||'+format('公斤','^3s')+'|'+format('磅','^5s')) j=20 for i in range(1,20,2): print('━'*25) print(format(i,'^5d')+'|'+format(i*2.2,'^6.1f')+'||'+format(j/2.2,'^5.2f')+'|'+format(j,'^5d')) j+=5 5.6: print('━'*25) print(format('英里','^3s')+'|'+format('公里','^5s')+'||'+format('英里','^3s')+'|'+format('公里','^5s')) j=20 for i in range(1,20): print('━'*25) print(format(i,'^5d')+'|'+format(i*1.609,'^6.3f')+'||'+format(j/1.609,'^5.3f')+'|'+format(j,'^5d')) j+=5 5.7: import math print('━'*25) print(format('度','^4s')+'|'+format('sin','^7s')+'|'+format('cos','^7s')) for i in range(0,370,10): print('━'*25) print(format(i,'^5d')+'|'+format(str(round(math.sin(math.radians(i)),4)),'^7s')+\ '|'+format(str(round(math.cos(math.radians(i)),4)),'^7s')) 5.8: import math print('━'*15) print(format('Number','^4s')+'|'+format('Square Root','^7s')) for i in range(0,21,2): print('━'*15) print(format(i,'^5d')+'|'+format(str(round(math.sqrt(i),4)),'^7s')) 5.9: fees=10000 growth=1+0.05 for i in range(1,11): fees=fees*growth print('第'+str(i+1)+'年的学费为:'+str(round(fees))) fees1=fees fouryears_total=fees for j in range(3): fees1*=growth fouryears_total+=fees1 print('第'+str(i+1)+'年的四年学费:'+str(round(fouryears_total))) 5.10: number=eval(input('number is ')) print(number) score=eval(input('score is')) print(score) print('max score is '+str(max(score))) 在cmd中输入‘cd C:\Users\beardool\Desktop\python code_wg’,再输入‘python 测试.py<score.txt’ 5.11: number=eval(input('number is ')) print(number) score=eval(input('score is')) print(score) score=sorted(score) print('the max scores is '+str(score[-1])+'and'+str(score[-2])) 定向输入与5.10一致 5.12:: count=0 for i in range(100,1000): if i % 5==0 and i % 6==0: count+=1 print(i,end=' ') if count % 10 ==0: print() 5.13: count=0 for i in range(100,1000): if (i % 5==0 or i % 6==0) and not (i % 5==0 and i % 6==0): count+=1 print(i,end=' ') if count % 10 ==0: print() 5.14: n=0 while n*n<12000: n+=1 else: print(n) 5.15: n=0 while n*n*n<12000: n+=1 else: print(n-1) 5.16: n1=eval(input('n1:')) n2=eval(input('n2:')) if n1 > n2 : n1,n2=n2,n1 d=n1 while (n1 % d !=0) or (n2 % d != 0): d-=1 print(d)
|