Python 派遣前参考
我遇到了一个错误,我已经试着解决了一段时间了Python 派遣前参考,python,reference,Python,Reference,我遇到了一个错误,我已经试着解决了一段时间了 if outerball.pos.x >= (self.r - 0.1): if self.rotations == 0: stopt = time.time ( ) onerot = stopt - startt print(onerot) self.rotations += 1
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=时间:%1.f%onerot+秒
UnboundLocalError:赋值前引用的局部变量“onerot”
我尝试过将变量全球化,但仍然没有产生任何效果。
有人能解释一下我如何解决这个问题吗
谢谢在您的情况下,只有当onerot满足嵌套if条件时,才会为其指定一个值,您需要为其指定一个默认值
onerot = 0 # ---> assign a value to onerot here
if ....:
if ...:
//your code
timey.text = "time: %1.f" % onerot + " seconds"
onerot仅在符合if条件时才赋值问自己:如果if块不执行,onerot的值是多少?我将指出与您有相同问题的地方:您有一个条件定义的变量。