I Just need some clarification on why while loop cannot use variables outside the function. It keeps saying the variables has not been assigned yet even though it has. Does it has something to do with precedence? On how python sets up variables? Or is it just unique to while loops?
test1 = True
test2 = True
while test1:
print("Test 1 " + str(test1))
test1 = False
def test_loop():
print("Test 2 " + str(test2))
# while test2:
# print("Test 2 " + str(test2))
# test2 = False
test_loop()
OUTPUT:
Test 1 True
Test 2 True
This works fine when the while loop in the function(test_loop)is commented out. Just to test if I can use the variable test2 and print it out. On which it can. But when I UNCOMMENT the while loop in the function, the variable test2 is unassigned. Which confuses the hell out of me.
I get this error:
in test_loop print("Test 2 " + str(test2))
UnboundLocalError: local variable 'test2' referenced before assignment