count = 0
def function():
count = count + 1
print(count)
Then we get: UnboundLocalError: local variable 'count' referenced before assignment
The reason this happens is because as soon as you write to a variable, that variable is automatically local to the function.
Here is the solution:
count = 0
def function():
global count #declare 'count' used in this function is the global one
count = count + 1
print(count)