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

renaming files in a directory using python or R -

html - outline-style different in chrome compared to firefox and internet explorer -

ruby on rails - Carrierwave Timeout -