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 Answers
+ 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)