2017-02-21 104 views
-5

我遇到了一个错误,我尝试了一段时间的处理。转让之前的参考

 if outerball.pos.x >= (self.r - 0.1): 
      if self.rotations == 0: 
       stopt = time.time () 
       onerot = stopt - startt 
       print(onerot) 
      self.rotations += 1 

     # update variable outputs 
     timey.text = "time: %1.f" % onerot + " seconds" 

误差为timey.text = "time: %1.f" % onerot + " seconds" UnboundLocalError: local variable 'onerot' referenced before assignment

我用尽全球化的变量,但它仍然未作出了区别。 有人可以解释我如何解决这个问题。

感谢

+1

'onerot'只值时分配符合'if条件' – haifzhan

+1

问问你自己:当你的if块没有执行时'onerot'的值是多少? – jmoerdyk

+0

我要指出你[这个问题](http://stackoverflow.com/questions/15367760/unboundlocalerror-local-variable-referenced-before-assignment-when-reading-from)哪些遭受同样的问题像你一样:你有一个有条件定义的变量。 –

回答

1

在你的情况onerot分配一个值,只有当它满足了嵌套if条件,您需要将默认值分配给它

onerot = 0 # ---> assign a value to onerot here 
if ....: 
    if ...: 
     //your code 
timey.text = "time: %1.f" % onerot + " seconds"