Below is a small section of code that illustrates my question:
index = 0
a = [1, 2, 3, 4]
def b():
print(index)
print(a)
def c():
print(index)
print(a)
while a[index] < 3:
index += 1
b()
c()
I get the following output:
0
[1, 2, 3, 4]
Traceback (most recent call last):
File "global.py", line 14, in <module>
c()
File "global.py", line 8, in c
print(index)
UnboundLocalError: local variable 'index' referenced before assignment
The function b
printed index
and a
as expected. However, function c
neither prints the two variables, nor do they seem to be accessible in the while
loop after the print statements.
What is causing the two variables to be accessible in b
but not in c
?