UnBoundLocalError:赋值前引用的局部变量(Python)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29533098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UnBoundLocalError: local variable referenced before assignment (Python)
提问by
I'm trying to create a function servo_to_quadrant
that returns the value servo_quadrant
.
我正在尝试创建一个servo_to_quadrant
返回 value的函数servo_quadrant
。
Questions similar to this one have involved there being an issue with a global variable outside of the function. I don't think that's the issue in this case, as the variable is only needed from within the function (although I could be wrong).
与此类似的问题涉及函数外部的全局变量存在问题。在这种情况下,我认为这不是问题,因为变量仅在函数内部才需要(尽管我可能是错的)。
Code:
代码:
def servo_to_quadrant(servo_val):
if servo_val < 0: 360 + servo_val
if servo_val >= 360: servo_val = servo_val - 360
if servo_val >= 0 and servo_val < 90: servo_quadrant = 1
if servo_val >= 90 and servo_val < 180: servo_quadrant = 2
if servo_val >= 180 and servo_val < 270: servo_quadrant = 3
if servo_val >= 270 and servo_val < 360: servo_quadrant = 4
return servo_quadrant
servo_val = -30
quadrant = servo_to_quadrant(servo_val)
print(quadrant)
Error:
错误:
Traceback (most recent call last):
File "test2.py", line 11, in <module>
quadrant = servo_to_quadrant(servo_val)
File "test2.py", line 8, in servo_to_quadrant
return servo_quadrant
UnboundLocalError: local variable 'servo_quadrant' referenced before assignment
采纳答案by Kasramvd
It's because you have assigned the variable servo_quadrant
under one of the preceding if
conditions in your function, and if none of the conditions return True, you will haven't any servo_quadrant
. For getting ride of this problem you need to initial this variable in your function.
这是因为您在函数servo_quadrant
中的上述if
条件之一下分配了变量,如果没有任何条件返回 True,您将没有任何servo_quadrant
. 为了解决这个问题,你需要在你的函数中初始化这个变量。
You can put servo_quadrant = 0
on top level of your function or you can check the value of the servo_quadrant
before you return anything :
你可以把servo_quadrant = 0
你的函数放在顶层,或者你可以servo_quadrant
在返回任何东西之前检查它的值:
if servo_quadrant :
return servo_quadrant
return None
Also Notethat you need to reassign variable servo_val
:
另请注意,您需要重新分配变量servo_val
:
if servo_val < 0: servo_val=360 + servo_val
Demo:
演示:
def servo_to_quadrant(servo_val):
servo_quadrant=0
if servo_val < 0: servo_val=360 + servo_val
if servo_val >= 360: servo_val = servo_val - 360
if servo_val >= 0 and servo_val < 90: servo_quadrant = 1
if servo_val >= 90 and servo_val < 180: servo_quadrant = 2
if servo_val >= 180 and servo_val < 270: servo_quadrant = 3
if servo_val >= 270 and servo_val < 360: servo_quadrant = 4
return servo_quadrant
servo_val = -30
quadrant = servo_to_quadrant(servo_val)
print quadrant
Result:
结果:
4
回答by artemdevel
This is because you try to modify servo_quadrant which isn't defined in your function. Python uses global scope by default if you just read a variable. So if you don't modify it everything will work fine. If you need to modify it just add global servo_quadrant
at the beginning of your function.
这是因为您尝试修改未在您的函数中定义的servo_quadrant。如果你只是读取一个变量,Python 默认使用全局作用域。所以如果你不修改它,一切都会正常工作。如果您需要修改它,只需global servo_quadrant
在函数的开头添加即可。
回答by khelwood
You need to make sure you assign servo_quadrant
in every path through your function before you try and return it.
servo_quadrant
在尝试返回函数之前,您需要确保在通过函数的每条路径中进行分配。
def servo_to_quadrant(servo_val):
if servo_val < 0: 360 + servo_val # <--- This does nothing
if servo_val >= 360: servo_val = servo_val - 360 # <--- This assumes servo_val < 720
if servo_val >= 0 and servo_val < 90: servo_quadrant = 1
if servo_val >= 90 and servo_val < 180: servo_quadrant = 2
if servo_val >= 180 and servo_val < 270: servo_quadrant = 3
if servo_val >= 270 and servo_val < 360: servo_quadrant = 4
# At this point, servo_quadrant may still not have been assigned
return servo_quadrant
If you want to move servo_val
into the range 0<=servo_val<360
, you can use the modulo operator.
And you can take advantage of the fact that if you return from a function, you don't need to keep checking the same conditions.
如果要移入servo_val
range 0<=servo_val<360
,可以使用模运算符。
并且您可以利用这样一个事实,即如果您从函数返回,则无需继续检查相同的条件。
def servo_to_quadrant(servo_val):
servo_val %= 360
if servo_val < 90: return 1
if servo_val < 180: return 2
if servo_val < 270: return 3
return 4