Python – Unexpected behaviour when accessing variable inside function that is defined outside function
Python – Unexpected behaviour when accessing variable inside function that is defined outside function Question: This results in UnboundLocalError: cannot access local variable ‘ans’ where it is not associated with a value. def a(): ans = 4 def traverse(): if ans == 2: pass if True: ans = 3 traverse() print(ans) a() This prints out …