Local (?) variable referenced before assignment [duplicate]

In order for you to modify test1 while inside a function you will need to do define test1 as a global variable, for example:

test1 = 0
def test_func():
    global test1 
    test1 += 1
test_func()

However, if you only need to read the global variable you can print it without using the keyword global, like so:

test1 = 0
def test_func():
    print(test1)
test_func()

But whenever you need to modify a global variable you must use the keyword global.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)