如何将参数传递给Python函数?

如何将参数传递给Python函数?,python,function,printing,formula,Python,Function,Printing,Formula,我正在尝试使用在线找到的这个函数来计算地球上两个纬度和经度点之间的距离 但是,我不知道如何传递纬度和经度的值,因为我替换了lat1,lon1和lat2,lon2,每次都会出错 在哪里输入纬度和经度的值 import math def distance(origin, destination): lat1, lon1 = origin lat2, lon2 = destination radius = 6371 # km dlat = math.radians(

我正在尝试使用在线找到的这个函数来计算地球上两个纬度和经度点之间的距离

但是,我不知道如何传递纬度和经度的值,因为我替换了
lat1
lon1
lat2
lon2
,每次都会出错

在哪里输入纬度和经度的值

import math

def distance(origin, destination):
    lat1, lon1 = origin
    lat2, lon2 = destination
    radius = 6371 # km

    dlat = math.radians(lat2-lat1)
    dlon = math.radians(lon2-lon1)
    a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
        * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
    d = radius * c

    return d
编辑: 例如,如果我有

lat1 = 20 and lon1 = 100 
lat2 = 30 and lon2 = 110 
当我用函数中的数字替换lon1时,为什么会失败?

如下使用:

distance((lat1, lon1), (lat2, lon2))
请注意,函数接收两个两元素元组(或者两个两元素列表,应该是相同的)作为参数,每个元组表示(纬度、经度)对。相当于:

origin = (lat1, lon1)
destination = (lat2, lon2)
distance(origin, destination)

您可以通过为每个参数传递元组来使用命名参数调用它,如:

distance(origin=origin_tuple, destination=dest_tuple)
在哪里


我仍然不知道如何使用它,因为当我尝试执行时,我总是会出错。假设我的lat1=20,lon1=100,lat2=30,lon2=110,那么我如何将这些数字放入方程和整个函数中。谢谢这样:
distance((20100),(30200))
Lopez请你编辑上面的初始代码,因为我正在尝试你的代码,它根本不起作用,因为当我输入元组时,我一直得到无效语法,正如你所说的那样,它对我有效,正如我在上面写的那样。你一定是做错了什么,但我的答案是正确的,我甚至用示例对它进行了测试,它返回
9818.324994648268
。感谢我现在让它工作起来,它确实是一个空格错误奥斯卡:)
origin_tuple = (origin_lat , origin_long)
dest_tuple = (dest_lat , dest_long)