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
servo_quadrant = 0if servo_val < 0: return 360 + servo_valservo_quadrant = 0seems to return0, as though it skips over all of the if functions. artemdevel - I'm trying to extrapolate the quadrant thatservo_valfalls into.