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

I'm still new to python and trying to figure it out. I tried to learn simple return methods, but it seems I can not understand it. I tried to find the distance between two points, and this is what I still have. If anyone could help me figure this out, that would be very helpful! Thanks!

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 
+6
source share
8 answers

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 
+12
source

Store result in 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) 
+1
source

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

Save the results of the function call in a 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
source
 print calculateDistance(2,4,6,8) 

Think of it as a function in mathematics. Place the function call in the position where you need its value. Alternatively, you can save the value in a variable:

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

(This does not work like math.)

0
source

I do not know what the โ€œreturn methodโ€ is - that you are here is a simple function.

What you do, however, calls it, but does nothing with the result: then printing the actual function itself, not the result.

You probably mean:

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

or even

 print calculateDistance(2,4,6,8) 
0
source

So, I'm not sure what kind of mistake this is.

If you want just:

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

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

Luck

0
source

Imagine that Python works in a function call ( calculateDistance(2, 4, 6, 8) ), evaluating the function and literally just replacing the line of code calculateDistance(2, 4, 6, 8) number returned by the function, say 7 .

Thus, typing calculateDistance(2, 4, 6, 8) in a line by itself will do the same as typing 7 in a line by itself. You need to do something with this value, for example, store it in a variable

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

or just print it immediately

print calculateDistance(2, 4, 6, 8)

0
source

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
source

All Articles