在 python 主程序中调用 google-map 函数并在 python 中传递参数

Call google-map Function in main python program and pass arguments in python

我需要定义一个包含两个参数的函数。参数在主 python 程序中提供,该程序调用其中的函数。该函数调用 googlemapsapi 来计算行程时间。主程序可以调用该函数,但不接受提供的参数。在没有提供参数的情况下执行的函数。

函数:

def travel_time(origin,destination):
return value

print("Below is Drive Time");
now = datetime.now()

directions_result_drive = gmaps.directions(origin,
                                 destination,
                                   mode="driving",                                     
                                 departure_time=now
                                )
value=round(float((directions_result_drive[0]['legs'][0]['duration']

['value'])/3600),1)

主要程序如下:

from travel_function import travel_time
origin = input("origin is:",)
destination = input("destination is:",)
print(travel_time(origin,destination));

根据提供的参数,它应该给出行程时间。例如Origin=NewYork, Destination="Boston", 返回的值应该是大约。 4 小时

看起来唯一的问题是 return 语句位于顶部,这应该有效:

def travel_time(origin, destination):

    print("Below is Drive Time")
    now = datetime.now()

    directions_result_drive = gmaps.directions(
        origin,
        destination,
        mode="driving",                                     
        departure_time=now
    )
    value = round(float((directions_result_drive[0]['legs'][0]['duration']['value'])/3600),1)
    return value