【发布时间】:2011-11-19 00:42:53
【问题描述】:
我在这里搜索过类似的问题,但找不到确切的答案。我不断收到“...在分配前引用”错误。
我试图将从一个函数返回的两个图形对象放入另一个函数的两个变量中。
我在早期版本中有基本完全相同的代码,它工作得很好。
我有一段代码:
if moveE:
moved_ctr, nT = MoveCharacter(ctr, spaceSize, borderSet, "east")
if moveW:
moved_ctr, nT = MoveCharacter(ctr, spaceSize, borderSet, "west")
moved_ctr.draw(board)
这就是 MoveCharacter() 的样子:
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
我不断收到错误:
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
【问题讨论】:
-
in an earlier version是什么? (面对模棱两可,拒绝猜测的诱惑)。