["python","python-3.x"]
Local (?) variable referenced before assignment

test1 = 0 def test_func(): test1 += 1 test_func() I am receiving the following error: UnboundLocalError: local variable 'test1' referenced before assignment. Error says that 'test1' is local variable but i thought that this variable is global So is it global or local and how to solve this error without passing global test1 as argument to test_func?
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.