0
Global and local variables
Can anyone please explain why below gives the error “ local variable ‘a’ referenced before assignment” ? a=[1,2,3] def add(n): a+=[n] add(4)
2 Respostas
+ 3
a=[1,2,3]
def add(n):
global a
a+=[n]
print(a)
add(4)
you have to use global keyword if you have to code like you asked in question or you have to code like choe said
+ 2
better to do this
a = [1,2,3]
def add(n):
a.append(n)
print(a)
add(4)