So I'm trying to follow along with the SICP lectures in Python, and have constructed the simple blackbox model for a Newtonian method of finding the square root approximations.
The code itself seems to work fine, but my function keeps returning a None? In the tryfor function below, I've made it such that it both prints the approximation AND returns it, such that the parent function can return the approximation.
I know from the print function that my code can find the right answer. However, when I write print(NewtonSqrt(2)), a None is returned - my approximation has not been 'returned' Confused as to why this is happening.
def NewtonSqrt(x):
def improve(guess):
return (guess + (x/guess)) / 2
def goodenough(guess):
if abs(guess - (x/guess)) < 0.00001:
return True
def tryfor(guess):
if goodenough(guess) == True:
print(guess)
return guess
else:
tryfor(improve(guess))
return tryfor(1)
print(NewtonSqrt(2))