def dx(fn, x, delta=0.001):
    return (fn(x + delta) - fn(x)) / delta

def solve(fn, value, x=0.5, maxtries=1000, maxerr=1e-05):
    for tries in range(maxtries):
        err = fn(x) - value
if abs(err) < maxerr:
    return x
slope = dx(fn, x)
x -= err / slope
raise ValueError('no solution found')
How can I solve equations in Python?