The third quiz of the course comes with two more situations to be solved.
The first of these consists in defining a function that finds the distance between two points, therefore, it should accept four parameters(an x and y for each point) and return one output value. To get the distance between two points you should first get the difference between the two values of x and the two values of y, respectively, get the square of each, add them and get the square root of that whole value. As in the pythagorean theorem, where c = √(a² + b²). Powers in python are expressed with the same symbol as multiplication, but doubled. For example, 6 ** 3 = 216. In this case, instead of a and b, we have (x1-x2) and (y1-x2).
Our defined function should look like this :
————————————————————————–
————————————————————————–
As you can see, I have imported the math module to use sqrt, short for square root. Although this could be easily replaced by **(1/2), as raising the power of a number to a half is the same as getting its square root.
This is our defined function, once we call it with a set of user input parameters, we’ll be able to test if our definition was correct. First, let’s type in those inputs :
————————————————————————–
Editor …
————————————————————————–
As you could have already noticed, I called float on each input, this is because the user may want to input decimals and not only integers, plus we want the square root’s output, and that of the function in general, to be as exact as possible.
Next, we should call our function with those inputs as values for the function’s parameters:
————————————————————————–
Editor :
————————————————————————–
Of course, we have to print to actually see