def find_root(a, b, tol, f): """ a < b are real numbers, f is continous on [a,b] tol > 0 is an error tolerance postc: returns a number x within tol of the root of f on [a,b]""" while b - a > 2*tol: m = (a + b)/2 if f(a)*f(m) < 0: b = m elif f(b)*f(m) < 0: a = m return m print(find_root(1, 2, .0001, lambda x:x*x - 2)) print(2**(.5)) print(find_root(1,1000, .0001, lambda x: x**3 - 100))