A_S A_S - 2 months ago 6
Python Question

Syntax error in python 3.4 function definition

I'm working with python 3.4 in pygame but I get a syntax error because of the second bracket in the function definition
How can I fix this?

def addVectors((angle1, length1), (angle2, length2)):
x = math.sin(angle1) * length1 + math.sin(angle2) * length2
y = math.cos(angle1) * length1 + math.cos(angle2) * length2
length = math.hypot(x, y)
angle = 0.5 * math.pi - math.atan2(y, x)
return (angle, length)

Answer

If your function is being passed in 2 tuples, you should extract the individual values from the tuples like so:

def addVectors(vector1, vector2):
    angle1, length1 = vector1
    angle2, length2 = vector2
    x  = math.sin(angle1) * length1 + math.sin(angle2) * length2
    y  = math.cos(angle1) * length1 + math.cos(angle2) * length2
    length = math.hypot(x, y)
    angle = 0.5 * math.pi - math.atan2(y, x)
    return (angle, length)