登录后查看本帖详细内容!
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
6.1:打印“五角数” def getPentahonalNumber(n): for i in range(1,n): number=int(i * ( 3 * i - 1) / 2) print(format(number,'5d'),end='\t') if i % 10 == 0: print() getPentahonalNumber(100) 6.2:n的每位数相加: def sumDigits(n): sum=0 count=0 while n != 0: number=n % 10 sum+=number n= n // 10 count+=1 return sum , count def main(n): sum,count=sumDigits(n) print('every digit sum is '+str(sum)) print(str(n)+' is '+str(count)+'bits digit') main(n=eval(input('n='))) 6.3:判断是否是回文数: def reverse(number): if isPalindrome(number)==number: print(str(number)+'is palindrome') else: print(str(number)+'is not palindrome') def isPalindrome(number): count=0 n='' while number != 0: n=n+str(number % 10) number=number // 10 count+=1 new_number=int(n) return new_number reverse(number=eval(input('enter an integer:'))) 6.4:输出反转数: def reverse(number): n='' while number != 0: n=n+str(number % 10) number=number // 10 new_number=int(n) return new_number print('the reverse number is ',reverse (number=eval(input('enter an integer:')))) 6.5三个数从小到大排列: def displaySortedNumbers(num1,num2,num3): if num1 > num2: num1,num2=num2,num1 if num2 > num3: num2,num3=num3,num2 if num1 > num2: num1,num2=num2,num1 return num1,num2,num3 num1,num2,num3=eval(input('enter three numbers:')) print(displaySortedNumbers(num1,num2,num3)) 6.6: def displayPattern(n): for i in range(1,n+1): for j in range(n-i,0,-1): print(' ',end='') for j in range(i,0,-1): print(j,end='') print() displayPattern(eval(input('enter an integer:'))) 6.7: def futureInvestmentValue(investmentAmount,monthlyInterestRate,years): value=investmentAmount*(1+monthlyInterestRate)**(years*12) return value def main(investmentAmount,monthlyInterestRate,years): print('Years Future Value') for i in range(1,years+1): print(format(i,'5d')+'\t',end='') print(format(futureInvestmentValue(investmentAmount,monthlyInterestRate,i),'.2f')) Amount=eval(input('enter the amount invested:')) monthlyrate=eval(input('enter the interest rate:')) years=eval(input('enter the interest years:')) main(Amount,monthlyrate,years) 6.8: def celsiusToFahrenheit(celsius): fahrenheit=(9/5)*celsius+32 return round(fahrenheit,1) def fahrenheitToCelsius(fahrenheit): celsius=(5/9)*(fahrenheit-32) return round(celsius,2) def title(): print('-'*70) print(' celsius | fahrenheit || fahrenheit | celsius') print('-'*70) def main(): title() j=120 for i in range(40,30,-1): print(format(float(i),'^12.1f'),'\t',end='') print(format(celsiusToFahrenheit(i),'^12.1f'),end='') print(format(float(j),'^12.1f'),'\t',end='') print(fahrenheitToCelsius(j)) j-=10 main() 6.9: 略 6.10: def isprime(number): isprime=True i=2 while i < number: if number % i ==0: isprime=False i+=1 return isprime count=0 for i in range(2,10001): if isprime(i): #print(format(i,'5d'),end='') count+=1 #ifcount % 10==0: #print() print(count)
|