How to calculate the distance between two points using return methods in python?
Asked Answered
E

10

6

I'm still new to python and have been trying to get the hang of it. I've been trying to learn simple return methods but I can't seem to get the hang of it. I have been trying to find the distance between two points and this is what I have so far. If anyone could help me figure this out it would be very helpful! Thank you!

import math

def calculateDistance(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     return dist

calculateDistance(2,4,6,8)

print calculateDistance
Eardrum answered 26/1, 2014 at 21:27 Comment(2)
try: print calculateDistance(2,4,6,8) ;-)Sparkle
Thank you! That worked! I can't believe I didn't think of it myself -.-Eardrum
I
12

Why don't you use math.hypot() to calculate the distance?

>>> import math
>>> p1 = (3, 5)  # point 1 coordinate
>>> p2 = (5, 7)  # point 2 coordinate
>>> math.hypot(p2[0] - p1[0], p2[1] - p1[1]) # Linear distance 
2.8284271247461903
Interwork answered 26/1, 2014 at 22:56 Comment(0)
K
1

Store the result in a variable

import math

def calculateDistance(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     return dist

distance = calculateDistance(2,4,6,8)

print distance

Or print the result directly

import math

def calculateDistance(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     return dist

print calculateDistance(2,4,6,8)
Kennard answered 26/1, 2014 at 21:30 Comment(0)
V
0

You have the right idea mostly (your function logic is correct) but the syntax for using the results of the function is not correct. To get the desired results you can do one of the following:

Save the results of the function call in the variable:

def calculateDistance(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     return dist

some_variable = calculateDistance(2,4,6,8)

print some_variable

or print directly:

def calculateDistance(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     return dist

print calculateDistance(2,4,6,8)
Visceral answered 26/1, 2014 at 21:30 Comment(0)
M
0
print calculateDistance(2,4,6,8)

Think of it like a function in math. Put the function call in the position where you need its value. Alternatively, you can store the value in a variable:

dist = calculateDistance(2,4,6,8)
print dist

(This does not work like math.)

Myrmecology answered 26/1, 2014 at 21:30 Comment(0)
R
0

I don't know what a "return method" is - what you have here is a simple function.

What you're doing, though, is calling it, but not doing anything with the result: then printing the actual function itself, rather than the result.

You probably mean:

distance = calculateDistance(2,4,6,8)
print distance

or even

print calculateDistance(2,4,6,8)
Roseberry answered 26/1, 2014 at 21:30 Comment(0)
T
0

So I'm not sure what your error is.

If you want the number just :

def calcdist(x, y, x1, y1):
     return math.sqrt((x-x1)**2 + (y2-y1)**2)

dist = calcdist(#, #, #, #)
print dist

Right now you are returning the function math.sqrt(...) so when you call calculate distance with 2, 4, 6, 8 you are returning an object that has the function and the 4 parameters, I think.

Good Luck

Trypanosome answered 26/1, 2014 at 21:33 Comment(0)
A
0

Imagine Python as running into the function call (calculateDistance(2, 4, 6, 8)), evaluating the function, and literally just replacing the line of code calculateDistance(2, 4, 6, 8) with the number that the function returns, let's say 7.

So typing calculateDistance(2, 4, 6, 8) on a line by itself will do just as much as typing 7 on a line by itself. You need to do something with that value, like store it in a variable

dist = calculateDistance(2, 4, 6, 8)

or just print it immediately

print calculateDistance(2, 4, 6, 8)

Analogous answered 26/1, 2014 at 21:35 Comment(0)
L
0

In Eclipse PyDev you can do it like here:

import math
p1 = (2, 4)  
p2 = (6, 8)  
dist = math.hypot(p2[0] - p1[0], p2[1] - p1[1])

print (dist)
Lumbricoid answered 28/5, 2017 at 14:38 Comment(0)
C
0
import math    
x1 = int(input("Enter x co-ordinate of 1st point:"))
y1 = int(input("Enter y co-ordinate of 1st point:"))

x2 = int(input("Enter x co-ordinate of 2nd point:"))
y2 = int(input("Enter y co-ordinate of 2nd point:"))

def distance_calc(a1,b1,a2,b2):
    
    d = math.sqrt((a2-a1)**2 + (b2-b1)**2)
    print(f"\nDistance: {d}")
    
distance_calc(x1,y1,x2,y2)
Contreras answered 12/5, 2021 at 17:22 Comment(1)
While this works, the answer doesn't address the issue in the question - the fact that the user wasn't correctly printing the return value of the function. Furthermore, the user specifically mentioned coding a function that returns a value, while your prints it instead.Overarch
E
-1

import math

x1 = int(input("Enter x co-ordinate of 1st point:")) y1 = int(input("Enter y co-ordinate of 1st point:"))

x2 = int(input("Enter x co-ordinate of 2nd point:")) y2 = int(input("Enter y co-ordinate of 2nd point:"))

def distance_calc(a1, b1, a2, b2): d = math.sqrt((a2 - a1) ** 2 + (b2 - b1) ** 2) print(f"\nDistance: {d}")

Call the function with the provided coordinates

distance_calc(x1, y1, x2, y2)

Exult answered 13/4 at 19:29 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Plasmagel

© 2022 - 2024 — McMap. All rights reserved.