python - How can I avoid this "variable referenced before assignment"? -


ran following problem. it's simple solution, , has been asked here before, couldn't find it.

def a():     lia = []     def b():         in lia:             += 1         lib = generatelist()         in lib:             -= 1         lia = lib      def generatelist():         return [1,2,3,4]      b()  a() 

unboundlocalerror: local variable 'lia' referenced before assignment

lia variable in b() function never initialized.

so, should edit code in followed way:

def a():     lia = []     def b(lia):         in lia:             += 1         lib = generatelist()         in lib:             -= 1         lia = lib      def generatelist():         return [1,2,3,4]      b(lia)  a() 

hope helped you!


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -