From this code:
COUNT = 0
def increment():
COUNT = COUNT + 1
increment()
I get the following error:
Traceback (most recent call last):
File "test.py", line 6, in <module>
increment()
File "test.py", line 4, in increment
COUNT = COUNT+1
UnboundLocalError: local variable 'COUNT' referenced before assignment
Why? How can I increment the global variable COUNT from inside the function?
globalamong beginners is usually a sign of bad design.globalyou can't modify the value of a global variable inside a function, you can only use it's value inside the function. But if you want to assign a new value to it then you've to use theglobalkeyword first.