我可能说错了标题。我想知道是否有可能为特定输入重复一个功能。显示代码将使其更容易解释。 这是代码:
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”并打印预期的出发时间。
如有需要,我可以提供更多详细信息。