How to address a global variable in a function

N

n179911

HI,

I have a global variable

// line 8
tx = 0

and then I have this function (start in line 12):
def handleTranslate(result):
print line
txStr, tyStr = result.group(1), result.group(2)
print txStr, tyStr

tx += int(txStr)
ty += int(tyStr)

return

But I am getting this error:
handleTranslate(result)
File "buildsvg.py", line 22, in handleTranslate
tx += int(txStr)
UnboundLocalError: local variable 'tx' referenced before assignment

How can I fix it?
I have assigned 'tx' to 0 in line 8. I don't understand why i get this
error.

Thank you for any help.
 
N

n179911

HI,

I have a global variable

// line 8
tx  = 0

and then I have this function (start in line 12):
def handleTranslate(result):
        print line
        txStr, tyStr = result.group(1), result.group(2)
        print txStr, tyStr

        tx += int(txStr)
        ty += int(tyStr)

        return

But I am getting this error:
    handleTranslate(result)
  File "buildsvg.py", line 22, in handleTranslate
    tx += int(txStr)
UnboundLocalError: local variable 'tx' referenced before assignment

How can I fix it?
I have assigned 'tx' to 0 in line 8. I don't understand why i get this
error.

Thank you for any help.

I figure out my problem

put this in my function:
global tx
 
C

Chris Rebert

HI,

I have a global variable

// line 8
tx  = 0

and then I have this function (start in line 12):
def handleTranslate(result):
       print line
       txStr, tyStr = result.group(1), result.group(2)
       print txStr, tyStr

       tx += int(txStr)
       ty += int(tyStr)

       return

But I am getting this error:
   handleTranslate(result)
 File "buildsvg.py", line 22, in handleTranslate
   tx += int(txStr)
UnboundLocalError: local variable 'tx' referenced before assignment

How can I fix it?
I have assigned 'tx' to 0 in line 8. I don't understand why i get this
error.

By default, Python assumes that any variables assigned to in a
function are local variables. To change this assumption, you need to
declare that you want to reference a global variable. This is done by
putting a `global` statement at the start of the function (i.e.
`global tx`).

In the future, you might STFW (in this case, for "UnboundLocalError")
before posting to the newsgroup, as you would have found several
explanations of this common newbie-biter.

Cheers,
Chris
 
A

Aahz

I have a global variable

// line 8
tx = 0

and then I have this function (start in line 12):
def handleTranslate(result):
print line
txStr, tyStr = result.group(1), result.group(2)
print txStr, tyStr

tx += int(txStr)
ty += int(tyStr)

return

BTW, you probably want to learn why global names are a bad idea, I don't
have time to explain that here (or point you at references). Going
through some of the online tutorials should address this.
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"...string iteration isn't about treating strings as sequences of strings,
it's about treating strings as sequences of characters. The fact that
characters are also strings is the reason we have problems, but characters
are strings for other good reasons." --Aahz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top