def find_root(a, b, tol, f): """prec: [a,b] is an interval and f is continuous on [a,b] tol is a positive tolerance for error. f changes sign on [a,b] returns a value in the interval that is within tol of the actual root.""" while abs(a - b) > 2*tol: mid = (b + a)/2 if f(mid)*f(a) < 0: b = mid else: a = mid return (b + a)/2 a = find_root(1, 2, .000001, lambda x: x*x - 2) b = 2**.5 print(f"error = {abs(a - b)}")