有没有一种方法可以使用来自Python中API的数据来为单独的输入循环功能?

我可能说错了标题。我想知道是否有可能为特定输入重复一个功能。显示代码将使其更容易解释。 这是代码:

from urllib.request import urlopen
import json

def askbot(bus_stop):

    if bus_stop == "CU2":
        url = urlopen("https://transportapi.com/v3/uk/bus/stop/43001053801/live.json?app_id=&app_key=&group=route&nextbuses=yes")
        data = json.loads(url.read().decode())
        json_str=json.dumps(data)
        resp=json.loads(json_str)  
        which_line = input("Which bus line would you like to know? ")
        if which_line == "10":
           print("Here is the current expected departure time at bus stop " + bus_stop + " for bus line " + resp['departures']['10'][0]['line_name'] + " heading to " + resp['departures']['10'][0]['direction'])
           print("Expected departure time: " + resp['departures']['10'][0]['expected_departure_time'])
           print("The next bus at bus stop " + bus_stop + "," + " for bus line " + resp['departures']['10'][1]['line_name'] + " will be heading to " + resp['departures']['10'][1]['direction'])
        elif which_line == "8":
            print
        else:
            print("That is not a valid line!")
    else:
        print("That bus stop does not exist!")
which_stop = input("Which bus stop timetable would you like to know? ")

askbot(which_stop)

我的问题是,它在询问用户which_line的地方,有没有办法使机器人自动搜索输入的公交线路的预期出发时间,而不必手动复制每条公交线路的代码?例如,如果我输入了公交车站“ CU2”的公交线路“ 8”,则机器人将检查该公交车站的API,并找到公交线路“ 8”并打印预期的出发时间。

如有需要,我可以提供更多详细信息。

klw3344 回答:有没有一种方法可以使用来自Python中API的数据来为单独的输入循环功能?

也许是沿着这些思路,假设索引与您上面的操作类似:

which_line = input("Which bus line would you like to know? ")

try:
    print("Here is the current expected departure time at bus stop " + bus_stop + " for bus line " +
        resp['departures'][which_line][0]['line_name'] + " heading to " + resp['departures'][which_line][0]['direction'])
    print("Expected departure time: " +
        resp['departures'][which_line][0]['expected_departure_time'])
    print("The next bus at bus stop " + bus_stop + "," + " for bus line " +
        resp['departures'][which_line][1]['line_name'] + " will be heading to " + resp['departures'][which_line][1]['direction'])
# If Line was not found
except KeyError as err:
    print("%s is not a valid line: %s" % (which_line,err))

这样,您将不必为所有行编写if语句,如果您在字典中输入的内容不正确,您将得到一个KeyError,因为找不到元素,这里我们将其捕获并在输入旁边输出错误用户给的。

我也建议不要打印输出,而是返回您要查找的信息,然后在主循环中将其打印出来。这样,您的函数便更具可重用性。另外,在捕获KeyError时,请考虑使用 raise (返回自定义错误)或 return (而不是像我在这里那样打印出来)。

,

您可以仅将行号作为变量插入。

which_line = input("Which bus line would you like to know? ")
print("Here is the current expected departure time at bus stop " + bus_stop + " for bus line " + resp['departures'][which_line][0]['line_name']

注意

resp['departures']['10'][0]['line_name']

成为

resp['departures'][which_line][0]['line_name']

这也取决于您使用的API。如果它可以接受行号,则可以在用户输入行号之后仅调用该行的数据后进行API调用。

,

我推断您之所以不重用which_line是因为您不想接受所有值。如果是这样,我建议您创建一个可接受值的列表,并将其作为您的条件。

existing_lines = [10,8,7,12,21]
if which_line in existing_lines:
    print("Here is the current expected departure time at bus stop " + bus_stop + " for bus line " + resp['departures'][which_line][0]['line_name']
本文链接:https://www.f2er.com/3163410.html

大家都在问