2017-02-21 51 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"