+ 4

Python Tuples exercise [SPOILER]

Hey everyone, I've been stuck in this exercise for half a day. Here's the exercise: You are given coordinates of 2 points and need to find the straight line distance between them. The coordinates are provided in a tuple, for example: p = (8, 11) PY The first value represents the x coordinate, while the second value represents the y coordinate of the point p. Complete the provided code to calculate and output the distance between the two given points. The linear distance is the square root of the square of the horizontal distance plus the square of the vertical distance between two points. The math.sqrt() function can be used to calculate the square root. This is what I tried: import math p1 = (23, -88) p2 = (6, 42) #your code goes here P1 = p1[0]**2 + p1[1]**2 P2 = p2[0]**2 + p2[1]**2 print(P1) print(P2) print(math.sqrt(P1+P2)) P1 = math.sqrt(p1[0]**2 + p1[1]**2) P2 = math.sqrt(p2[0]**2 + p2[1]**2) print(P1+P2) # 1st block output: 100.36433629531956 # 2nd block output: 133.38244020598597 The expected output: 131.10682667199293 I'm not that far from the result but stills isn't what is expected. Anyone, know what am I doing wrong?

18th Apr 2021, 10:06 AM
Da Silva Miguel
Da Silva Miguel - avatar
9 odpowiedzi
+ 10
As far as I know the thoery should be: root((x2-x1)^2+(y2-y1)^2) For my guess,your code is not following this theory. Maybe the code will be like this: https://code.sololearn.com/c211a1a19A10
18th Apr 2021, 10:14 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 5
Here is the - what i think - most selfexplaining code: import math p1 = (23, -88) p2 = (6, 42) x = (p1[0]-p2[0]) y = (p1[1]-p2[1]) print(math.sqrt((x)**2 + (y)**2)) Ask if u have questions!
17th Nov 2021, 4:26 PM
Stefan Bartl
Stefan Bartl - avatar
+ 2
1. find the distance between the x coordinates: To do this, we have to find the difference between the two values and store it in a variable. x = p1[0] - p2[0]. 2. Find the distance between the y coordinates: Again, find the difference between the two values. The answer will be negative as -88 is negative so we need to convert it to positive to find the difference. In python, we use the function abs() to do this. 3. Find the square of each number: x**2, y**2 4. Find the square root of the sum of the 2 numbers: math.sqrt(x+y) Here is the completed code: import math p1 = (23, -88) p2 = (6, 42) #your code goes here x = p1[0] - p2[0] y = abs(p1[1] - p2[1]) print(math.sqrt(x**2 + y**2))
26th Oct 2021, 12:03 PM
Eesa Adam
Eesa Adam - avatar
+ 2
My code has a few more lines, but I thought it was pretty didactic. ================= import math p1 = (23, -88) p2 = (6, 42) dx = (p1[0]-p2[0]) dy = (p1[1]-p2[1]) print(math.sqrt((dx)**2 + (dy)**2)) =================
28th Oct 2021, 12:46 AM
Demétrio M.
Demétrio M. - avatar
+ 1
@The future is now thanks to science, i tried and it worked perfectly.
18th Apr 2021, 10:27 AM
Da Silva Miguel
Da Silva Miguel - avatar
0
A negative value made some of the other answers here kick back a math domain error. My workaround is giving the absolute value of the difference as posted below. Worked like a charm after that. ''' import math p1 = (23, -88) p2 = (6, 42) distance = math.sqrt((abs(p2[0]-p1[0])**2)+(abs(p2[1]-p1[1])**2)) print(distance) '''
15th Dec 2021, 8:54 PM
Matt Gehrls
Matt Gehrls - avatar
0
import math p1 = (23, -88) p2 = (6, 42) #your code goes here x = p1[0]-p2[0] y = p1[1]-p2[1] square_horizontal = x**2 square_vertical = y**2 print(math.sqrt(square_horizontal+square_vertical))
7th Oct 2022, 10:46 AM
Pareshkumar Chaudhari
- 1
Correct in code import math p1 = (23, -88) p2 = (6, 42) distance = math.sqrt((p2[0]-p1[0])**2+(p2[1]-p1[1])**2) print(distance)
22nd Jul 2021, 3:59 AM
Hugues Rodrigue Tha Andela
Hugues Rodrigue Tha Andela - avatar