0
gCharlie = 0
gJeff = 0

def Bob ():
    Charlie = gCharlie
    Jeff = gJeff
    Number = int(input("Hello and Welcome to Charlie's Number Guessing Game. Enter a nonnegative number from 0 to 10 not counting 0. "))

This code gives me this error in Python 3.2:

UnboundLocalError: local variable 'gCharlie' referenced before assignment   

What does this local variable error mean?

2
  • 4
    I ran this code myself and got not such errors. Maybe provide the code that's calling this function? Is it in a separate file? How are you importing it if so? etc. Commented Jan 23, 2012 at 3:50
  • 3
    I tried the above in two different versions of Python and it ran perfectly. Commented Jan 23, 2012 at 3:51

4 Answers 4

3

inside the Scope of your function you must have reassigned gJeff and gCharlie, which created them as new local variables. To tell python that you're using the globals, change the top of your function to look like this.

def Bob():
    global gCharlie
    global gJeff
    Charlie=gCharlie

without telling python that you're using the globals, it tries to reference local gCharlie and gJeff variables, which as it says, have not been assigned at that point in your function. The reason people are getting it to work is because they're using only the code you've posted. You can reference globals without explicitly saying so ONLY if you don't change their values within the function referencing them.

As a rule python searches in this order for a variable name: local scope, any def it is nested inside of, global, built ins. Bbecause gJeff and gCharlie are local variables in your function, it stops there, unless you tell it otherwise.

If you want to see this in action try to look at this

 x=5

 def useX():
     x=0     #this is a local variable
     print "in function: ", x

 def main():
     print "in main(1): ", x
     useX()
     print "in main(2): ", x

 main()

this will output

in main(1):  5 
in function:  0
in main(2):  5

because within the function, x is created as a new local variable. Adding a global x statement to the useX function would change the last line to print "0" instead of "5"

Sign up to request clarification or add additional context in comments.

3 Comments

"it doesn't recognize the globals by default." is misleading e.g., every module name that you introduce via import some_module at a module-level is an ordinary global that you obviously can use inside any function. You don't need global if you don't change their bindings inside the function, example. If you do need it then you should probably refactor the function into a method and the global into an instance attribute.
Even more obvious example is the function names. You don't need global useX to call useX() inside main().
You're right. I meant the first part to be a simplification for his sake. I edited that all out so it should be good now.
2

It might be due to there is gCharlie = inside a function (note: the first letter is g).

Use parameters instead of globals:

def bob(charlie=0, jeff=0):
    number = int(input("..."))
    # ...

bob(charlie=3)

Comments

2

It means that you're assigning to gCharlie in the part of the function you didn't show, and so the Python compiler has marked it as a local variable. As such, you're accessing it before it exists. Use nonlocal or global to solve.

Comments

1

Two previous answers here are correct, but both are a bit unclear. I'll show with some examples:

The code you show will work fine:

>>> gCharlie = "Global!"
>>> 
>>> def foo():
...     print(gCharlie)
... 
>>> foo()
Global!
>>> print(gCharlie)
Global!

So that's not the problem at all. However, you can't assign global variables inside a function:

>>> gCharlie = "Global!"
>>> def foo():
...     gCharlie = "Local!"
...     print(gCharlie)
... 
>>> foo()
Local!
>>> print(gCharlie)
Global!

As you see, the global variable gCharlie did not change. This is because you did not modify it, you created a new local variable, with the same name. And this is the cause of the error:

>>> gCharlie = "Global!"
>>> def foo():
...     oldCharlie = gCharlie
...     gCharlie = "Local!"
...     print(gCharlie)
... 
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in foo
UnboundLocalError: local variable 'gCharlie' referenced before assignment

The hint is in the error. It says local variable gCharlie. The problem is not the inability to access the global gCharlie, but that the local one hasn't been created yet.

The fix is to specify that you don't want to create a local variable, but that you want to modify the global one. You do this with the global keyword.

>>> gCharlie = "Global!"
>>> def foo():
...     global gCharlie
...     oldCharlie = gCharlie
...     gCharlie = "Local!"
...     print(gCharlie)
... 
>>> foo()
Local!
>>> print(gCharlie)
Local!

As you see now, you modified the global variable.

That said, global variables are usually a bad idea. Avoid them. Try to pass in the variables as parameters instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.