I have searched through similar questions on here, and cannot find an exact answer. I keep getting a “…referenced before assignment” error.
I am trying to put two graphics objects returned from one function into two variables in another function.
I had basically the exact same piece of code in an earlier version, and it worked just fine.
I have the piece of code:
if moveE:
moved_ctr, nT = MoveCharacter(ctr, spaceSize, borderSet, "east")
if moveW:
moved_ctr, nT = MoveCharacter(ctr, spaceSize, borderSet, "west")
moved_ctr.draw(board)
This is what MoveCharacter() looks like:
def MoveCharacter(character, spSz, bdSet, direction):
x1 = character.getCenter().getX()
x2 = bdSet
y1 = character.getCenter().getY()
y2 = bdSet
notifyText = Text(Point(100, 100), "")
character.undraw()
distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
if direction == "east":
if distance < spSz:
character.move(spSz, 0)
else:
notifyText.setText("You cannot move right from where you currently are.")
if direction == "west":
if distance > spSz:
character.move(-1 * spSz, 0)
else:
notifyText.setText("You cannot move left from where you currently are.")
return character, notifyText
And I keep getting the error:
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
Grid()
File "<pyshell#24>", line 70, in Grid
moved_ctr.draw(board)
UnboundLocalError: local variable 'moved_ctr' referenced before assignment
What happens before
if moveE:?Most likely,
moveEandmoveWare falsey.