Think Python 练习答案

您所在的位置:网站首页 think1课本答案 Think Python 练习答案

Think Python 练习答案

2024-03-07 03:46| 来源: 网络整理| 查看: 265

对于书中没有提供答案的习题,我会在此处把自己的答案贴上来。欢迎大家围观指正。有什么不明白的也可以提问。 3-1

def right_justify(s): length=70-len(s) print(length*' '+s) right_justify('monty')

5-1

import time t=time.time() s=int(t%86400) m=int(s/60) h=int(m/60)+8 print(h,'时',m%60,'分',s%60,'秒')

我国时区是东八区,需要加上8 5-2

def check_fermat(a,b,c,n): if n>2 and a*n+b**n==c**n: print('天哪,费马弄错了!') else: print('不,那样不行!') a=input('a=') a=int(a) b=input('b=') b=int(b) c=input('c=') c=int(c) n=input('n=') n=int(n) check_fermat(a,b,c,n)

默认input的是str格式,而str格式不能和int格式比大小,所以要用int语句给变量赋值

5-3

def is_triangle(a,b,c): if a+b>c or a+c>b or b+c>a: print('Yes') else: print('No') a=input('a=') b=input('b=') c=input('c=') is_triangle(a,b,c)

6-1 9 90 8100 6-2

def A(m,n): if m==0: return n+1 if m>0 and n==0: return A(m-1,1) if m>0 and n>0: return A(m-1,A(m,n-1)) print A(3,4)

7-1

import math def square_root(a): x = a y = 0 while True: y = (x + a/x) / 2.0 if abs(y-x) < 1e-4: break x = y return y def test_square_root(): print('a\tmysqrt(a)\tmath.sqrt(a)\tdiff') print('-\t---------\t------------\t----') for a in range(1, 10): mysqrt = square_root(a) mathsqrt = math.sqrt(a) print('%.1lf\t%lf\t%lf\t%lf' % (a, mysqrt, mathsqrt, abs(mysqrt-mathsqrt))) test_square_root()


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3