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
2
  • 6
    try: print calculateDistance(2,4,6,8) ;-)
    – mgilson
    Commented Jan 26, 2014 at 21:29
  • Thank you! That worked! I can't believe I didn't think of it myself -.-
    – User1222
    Commented Jan 26, 2014 at 21:31

10 Answers 10

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

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

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

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)
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)
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.
    – piit79
    Commented May 12, 2021 at 19:42
-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)

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.
    – Community Bot
    Commented Apr 14 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.