help please

Erik Max Francis max at alcyone.com
Sat Feb 12 19:37:27 EST 2005


gargonx at gmail.com wrote:

> UnboundLocalError: local variable 't2' referenced before assignment

	...

> t2=""
> 
> def Proc(text): # "text" is some random text or use OrigText
>     for word in text:
>         for letter in word:
>             if letter in std.keys():
>                 letter=std[letter]
>                 t2=t2+letter  # the problem is referene to this
>             elif letter in ext.keys():
>                 letter=ext[letter]
>                 t2=t2+letter
>             elif letter in punc.keys():
>                 letter=punc[letter]
>                 t2=t2+letter
> 
> can anyone figure out why t2 is not being used properly?

The problem is that you're not making it clear to Python that you want 
the t2 mentioned in Proc to reference the global t2, rather than a 
local.  It's concluding the latter, whereas you mean the former.  You 
can probably see that as a local, t2 is indeed referenced before it's 
assigned (t2 = t2 + letter).  The fix is to declare t2 global at the top 
of Proc:

	def Proc(text):
	    global t2
	    ...

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   I am become death, the destroyer of worlds.
   -- J. Robert Oppenheimer (quoting Hindu legend)



More information about the Python-list mailing list