UnboundLocalError: local variable 'X' referenced before assignment Python for simbols fixer -


i have simbols fixer in python latex made code , dont know why shows

traceback (most recent call last):   file "c:\users\stark's probook\desktop\prueba.py", line 92, in <module>     bracketfind()   file "c:\users\stark's probook\desktop\prueba.py", line 30, in bracketfind     h in archivo: unboundlocalerror: local variable 'archivo' referenced before assignment 

this code python 2.7

# -*- coding: cp1252 -*-  archivo = open("test.txt", "r") texto = archivo.readlines() archivo = "".join(texto) opening = 0 closes = 0 print archivo        def count():     return contador('{')-contador('}')  def contador(simbolo):     conta = 0     h in archivo:         if simbolo == h:             conta= conta+1     return conta  def insert(string, index):     return string[:index] + '.' + string[index:]  def remove(string, index):     return string[:index-1] + '.' + string[index+1:]  def bracketfind():      opening = 0     closes = 0     h in archivo:         if '{' == h:             opening = opening+1         elif '}' ==h:             closes = closes+1      print "abiertas ({) "+ str(opening) + " cerradas (}) "+str(closes)     position = -1     startreplacing = false     openposition = -1     while true:         position = -1         startreplacing = false         openposition = -1         if count() == 0:             print "revision exitosa!!, no existe ninguna { sin cerrar " +"si tienes problemas de compilacion no es por esta razón. " + "revisa tu codigo en busca de otro error"             return true         if contador('{') == 0 , contador('}')>0:             break         if contador('{') > 0 , contador('}') == 0:             break         character in archivo:             position = position+1             if character == '{':                 openposition = position                 startreplacing = true             if startreplacing:                 if character == '}':                     try:                         archivo = remove(archivo,openposition)                         archivo = insert(archivo,openposition)                         archivo = remove(archivo,position)                         archivo = insert(archivo,position)                     except:                         break     ipos = -1     icount = -1     itarget = 0     itype = 0      if '{' in archivo:         itarget = archivo.rfind('{')         print itarget         itype = 1     elif '}' in archivo:             itarget = archivo.rfind('}')             itype = 2     if itarget == -1:         return true     if itype == 1:         print "la llave que abre (" , itarget , ") quizas es inecesaria o parece no estar cerrada"     elif itype == 2:             print "la llave que cierra (" , itarget , ") quizas es inecesaria o parece no estar cerrada"     character in archivo:         ipos = ipos+1         if character == '{' or character == '}':             icount = icount+1             if(icount == itarget):                print "la llave ",icount , "parece tener error"     return true  print count() bracketfind() 

do have idea causing this? dont understand why showing if @ begin of execution prints 'archivo'

this has how python handles scopes. initially, archivo global variable, defined outside function or class; can accessed scope. bracketfind() includes several definitions archivo, ie 'archivo = remove(archivo,openposition)'. causes archivo revert local scope function; global variable trying refer no longer accessible.

the easiest way fix add line 'global archivo' near beginning of bracketfind(), more robust solution rework code archivo no longer global variable.


Comments

Popular posts from this blog

python - Mongodb How to add addtional information when aggregating? -

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

java - Incorrect order of records in M-M relationship in hibernate -