简体   繁体   中英

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

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

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

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)

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)
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.)

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)

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

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)

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)
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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM