如何使用python中的返回方法计算两点之间的距离?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21369449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:42:30  来源:igfitidea点击:

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

pythonreturndistance

提问by User1222

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!

我对 python 还是个新手,一直在努力掌握它。我一直在尝试学习简单的返回方法,但我似乎无法掌握它。我一直在试图找到两点之间的距离,这就是我到目前为止所拥有的。如果有人能帮我解决这个问题,那将非常有帮助!谢谢!

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

回答by shuttle87

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)

回答by YXD

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)

回答by user2357112 supports Monica

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

(这不像数学那样工作。)

回答by Daniel Roseman

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)

回答by arthur.00

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.

现在您正在返回该函数,math.sqrt(...)因此当您使用 2、4、6、8 调用计算距离时,您将返回一个具有该函数和 4 个参数的对象,我认为。

Good Luck

祝你好运

回答by Eli Rose -- REINSTATE MONICA

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.

想象一下 Python 运行到函数调用 ( calculateDistance(2, 4, 6, 8)) 中,评估该函数,并且实际上只是将代码行替换calculateDistance(2, 4, 6, 8)为函数返回的数字,比方说7

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

因此,单独calculateDistance(2, 4, 6, 8)在一行上键入与单独7在一行上键入一样多。您需要对该值执行某些操作,例如将其存储在变量中

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

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

or just print it immediately

或立即打印

print calculateDistance(2, 4, 6, 8)

print calculateDistance(2, 4, 6, 8)

回答by Fernando

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

为什么不使用 math.hypot() 来计算距离?

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

回答by user3900428

In Eclipse PyDev you can do it like here:

在 Eclipse PyDev 中,您可以这样做:

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

print (dist)