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# - ajax - How to receive data both html and json from server? -

c# - SharpDX Toolkit models rendering inside out -