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
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
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))