如何使用用户输入访问变量?

2024-09-29 21:37:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个程序,在这个程序中,我想提示用户选择纽约市行政区,并使用该行政区的通用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()的一些变体来获取相应的自治区数据

this answer中我知道,如果我想使用输入创建变量,我可以这样做。是否有方法访问变量


Tags: 代码用户程序dfinputlocgps行政区
1条回答
网友
1楼 · 发布于 2024-09-29 21:37:00

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

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}')

相关问题 更多 >

    热门问题