2

I'm trying to create a function servo_to_quadrant that returns the 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
3
  • Just initialize it to some value at the start of function. Like this servo_quadrant = 0 Commented Apr 9, 2015 at 8:09
  • Probably you can just return required values instead of using servo_quadrant variable. Like if servo_val < 0: return 360 + servo_val Commented Apr 9, 2015 at 8:15
  • Zlopez - servo_quadrant = 0 seems to return 0, as though it skips over all of the if functions. artemdevel - I'm trying to extrapolate the quadrant that servo_val falls into. Commented Apr 9, 2015 at 8:29

3 Answers 3

6

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.

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 :

if servo_quadrant :
    return servo_quadrant
return None

Also Note that you need to reassign variable 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
Sign up to request clarification or add additional context in comments.

3 Comments

I've added servo_quadrant = 0 in the first line of the function, but it seems to return 0 - as though it has skipped over the if statements.The first two if statements modify servo_val to be within the ranges 0 to 360, in order to work with the if statements that follow.
@l87q4t you need to reassign it in your first if : so change the condition to servo_val=360 + servo_val
oops...silly me. It works now (without needing servo_quadrant=0). Thanks :)
1

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.

3 Comments

Does adding global servo_quadrant mean that it becomes a global variable? I was hoping to keep it local to the function, as I intend to run multiple instances of the function at once (this code is part of a larger code).
yes, it will be in a global scope, in your case it is better to initialize it to some value as mentioned in other comments.
Others have pointed out that the problem was that I forgot to add the code that would modify servo_val to fit within 0 and 360 (in the first two if statements), but thank you anyways.
1

You need to make sure you assign servo_quadrant in every path through your function before you try and return it.

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.

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

3 Comments

I added the first two if statements in order to modify servo_val so that it's value is within 0 and 360 in order to work with the if functions that follow it.
This line: if servo_val < 0: 360 + servo_val is not modifying servo_val. This line: if servo_val >= 360: servo_val = servo_val - 360 doesn't necessarily move servo_val into the expected range, unless you know that servo_val can't be more than 720.
I should have paid more attention. Thanks though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.