Hi all,
I am struggling to understand why this code runs correctly and I can't seem to find an answer online.
1) does python search for global value (and use them) by default if local not found? (or does it only do this if the input args are empty?
2) why if you use a while loop like below, do I get the error of not finding the global variable, but don't get that error for finding a,b etc.?
ERROR: "UnboundLocalError: local variable 'is_true' referenced before assignment" :
Thanks so much in advance.
I am struggling to understand why this code runs correctly and I can't seem to find an answer online.
a = 10 b = 5 def function(): new_local_variable = a other_local_variable = new_local_variable * b print(other_local_variable) function()In the video I watched it runs correctly (prints 'other local variable') and uses the global value of 'a' inside the function without input arguments in the function definition OR calling the global variable 'a' into the function.
1) does python search for global value (and use them) by default if local not found? (or does it only do this if the input args are empty?
2) why if you use a while loop like below, do I get the error of not finding the global variable, but don't get that error for finding a,b etc.?
ERROR: "UnboundLocalError: local variable 'is_true' referenced before assignment" :
a = 10 b = 5 is_true = True def function(): iter = 0 while is_true: new_local_variable = a other_local_variable = new_local_variable * b print(other_local_variable) iter += 1 if iter == 20: is_true = False function()Am I being really dumb?
Thanks so much in advance.