0

In beginner terms, why does this give me an error?

hey = 1
def addition(x):
    hey = hey + x
    return hey
print(addition(1))

I get this error message:

UnboundLocalError: local variable 'hey' referenced before assignment
J. Park
  • 1
  • 1
  • Its outside the scope of the addition function. They are essentially two different "hey" s. They look exactly the same but python doesn't know or think that. – Sam Feb 22 '19 at 19:27

1 Answers1

1

Definitely a duplicate but to answer the question you should look into global variables.

If you want the code above to work you need to tell python that you are using hey as a global variable such as:

hey = 1
def addition(x):
    global hey 
    hey = hey + x
    return hey
print(addition(1))