1

我正在创建一个程序,我想在其中提示用户纽约市行政区,并使用所述行政区的一般 GPS 坐标。我的部分代码是

df.loc[0,'BOROUGH'] = input('Borough:\n')
manhattan = [40.7831, -73.9712]
brooklyn = [40.6782, -73.9442]
staten_island = [40.5795, -74.1502]
queens = [40.7282, -73.7949]
bronx = [40.8448 ,-73.8648]

我现在想使用输入响应来访问适当的坐标。例如,如果用户输入“Manhattan”,我可以使用一些变体input().lower()来获取相应的自治市镇数据。

我从这个答案中知道,如果我想使用输入创建一个变量,我可以这样做。有没有办法访问变量?

4

1 回答 1

0

这是给定用户输入从集合中挑选一些数据的一种方法:

boroughs = {'manhattan':[40.7831, -73.9712],
'brooklyn': [40.6782, -73.9442],
'staten_island': [40.5795, -74.1502],
'queens': [40.7282, -73.7949],
'bronx': [40.8448 ,-73.8648]
}

while True:
    borough = input('Please enter a borough name: ').lower()
    if borough in boroughs:
        print(f'GPS coordinates of {borough} are {boroughs[borough]}')
    else:
        print(f'Unknown borough: {borough}')
于 2020-12-06T21:34:04.133 回答