Aug-02-2017, 09:43 PM
Hey guys, I was trying to write a function that takes as an argument a number, and according to Newton's formula that states we can estimate the square root of a number by trying several times to "estimate" it. This is the piece of code I wrote:
def main(): number = int(input("Insert the number:")) estimation = 1 if number == "": print("Goodbye!") elif number < 0: print("Error: negative number") else: def estim(someNumber): estimation = (estimation + number / estimation) / 2 return number while True: tollerance = 0.000000001 estim(number) difference = abs(number - estim(number) **2) if difference <= tollerance: break return estim(x)The problem is: even if I assign to the variable estimation a value of 1 in the first lines, python gives me this errors:
line 19, in main estim(number)
line 15, in estim estimation = (estimation + number / estimation) / 2 UnboundLocalError: local variable 'estimation' referenced before assignment