Friedrich_Cheng94
Friedrich_Cheng94

Reputation: 43

How to construct a function that passes a tuple-like argument as its input?

I'm a beginner in Python who seeks to create a function that passes a point of the form (x, y) and returns the absolute value of the point and a specific line. As required, I have to define a function like this

def calculate_error(m, b, (x_point, y_point) ):
    error = abs(get_y(m, b, x_point) - y_point)

I have a trouble passing the point to the function above so that I cannot manipulate either the x-coordinate and the y-coordinate inside the function. Am I supposed to treat the point (x,y) as a tuple or a list?

Upvotes: 1

Views: 47

Answers (1)

Selcuk
Selcuk

Reputation: 59219

You can pass the tuple as a single variable and unpack it inside the function:

def calculate_error(m, b, point):
    x_point, y_point = point
    error = abs(get_y(m, b, x_point) - y_point)

you can then call it passing a tuple:

print(calculate_error(1, 2, (3, 4)))

Upvotes: 2

Related Questions