As the title suggests, when I try to execute this function:
num = 3
def add_num():
num += 6
print(num)
add_num()
print(num)
it gives this error:
Traceback (most recent call last):
File "C:/Users/Sean/Desktop/Programming/Python_prac/test.py", line 9, in <module>
add_num()
File "C:/Users/Sean/Desktop/Programming/Python_prac/test.py", line 5, in add_num
num += 6
UnboundLocalError: local variable 'num' referenced before assignment
What confuses me is that you can reference "num" within the function once you don't try to assign it to itself. However, wouldn't the "num" variable created in the function just point to another location in memory and this not be an issue?
Could someone explain this behavior.