Hello everyone! I have been going through the python 3 lessons in order and just wanted to check my understanding for a part of the project. I have been having issues with defining
y_point for this portion of the project:
Reggie wants to try a bunch of different
bvalues and see which line produces the least error.
To calculate error between a point and a line, he wants a function called
calculate_error(), which will take in
b, and an [x, y] point called
pointand return the distance between the line and the point.
To find the distance:
- Get the x-value from the point and store it in a variable called
- Get the y-value from the point and store it in a variable called
My thought process when trying to solve this task was as follows:
Originally, due to the instructions saying "an [x, y] point called
point" I thought I was dealing with a
list . However, looking at the input later in the project, I realised that actually you will be dealing with a
Regardless I attempted to write codes like:
def calculate_error(m,b,point): point = x_point point = y_point
thinking that if your input was for example
calculate_error (1, 1, (2, 3)), the function would then
- extract element with index 0 from the tuple and set it as the
- extract element with index 1 from the tuple and set it as the
However I got a syntax error.
After much struggling, I opened the provided solution which showed this:
def calculate_error(m,b,point): x_point, y_point = point return abs(get_y(m, b, x_point) - y_point)
So my main questions are:
from the proposed solution, is the reason why
x_point, y_point = pointworks because this is the way you unpack data from an input that is a tuple?
why is my initial thought process wrong? - i.e.
point = x_point point = y_point
- if the input was a list instead, would
x_point, y_point = pointstill be correct? - i.e. is unpacking a tuple the same as unpacking a list?