Python Forum
variable referenced before assignment
Thread Rating:
  • 3 Vote(s) - 3.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
variable referenced before assignment
#1
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
Reply
#2
Each function has it's own scope. The function, which is called in main() could not access to the local variables of main.

The function estim does not see the variable estimation. How could a function evaluate this statement, when estimation is unknown?
estimation = (estimation + number / estimation) / 2
This error happens also, when you've assigned a global variable (on module level) and try to assign a new object to this name inside a function. You can use global, but you should avoid this. You can assign the object, which is referenced by a global name (variable) to a local name inside your function. Then you can do afterwards a new assignment to the local variable, which don't have an effect on the global variable.

In Python you're referencing by name.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
 
estimation = (estimation + number / estimation) / 2
Here the right side of the equation is evaluated before the asignment to the left side. Always. But the variable is not defined yet so estimation on the right doesn't exists when you try to use it.

As @DeaD_EyE said in your case you are trying to use a global variable. But in a function all variables are local. You can pass it as an argument.

global_var = 1

def func(var):
    print(var)

func(global_var)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Thanks guys, as you advised I assigned the variable inside that second function and I put a while loop after that, and it worked.
def main():
    number = int(input("Insert the number:"))
    if number == "":
        print("Goodbye!")
    elif number < 0:
        print("Error: negative number")
    else:
        def estim(someNumber):
            estimation = 1
            while True:
                estimation = (estimation + someNumber / estimation) / 2
                tollerance = 0.000000001
                difference = abs(number - estimation **2)
                if difference <= tollerance:
                    break
            return estimation
    return estim(number)
Reply
#5
If number is less than 0 (or blank), estim won't be defined, so calling it would be an error.  I'm not sure why you're defining it anyway, just use a while loop directly:
def main():
    number = int(input("Insert the number:"))
    if number == "":
        print("Goodbye!")
    elif number < 0:
        print("Error: negative number")
    else:
        estimation = 1
        while True:
            estimation = (estimation + number / estimation) / 2
            tollerance = 0.000000001
            difference = abs(number - estimation **2)
            if difference <= tollerance:
                break
        number = estimation
    return number
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rounding exercise: UnboundLocalError: local variable referenced before assignment Drone4four 5 3,336 Sep-06-2020, 09:01 AM
Last Post: ibreeden
  UnboundLocalError: local variable 'a' referenced before assignment fad3r 3 16,489 Jun-20-2018, 05:43 PM
Last Post: nilamo
  why am I getting "local variable 'x' referenced before assignment"? wlsa 6 9,066 Jun-16-2018, 05:31 PM
Last Post: buran
  local variable 'l' referenced before assignment... darkreaper1959 4 7,374 Jan-21-2017, 08:16 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020