Skip to main content
 首页 » 编程设计

python之如何正确处理python错误

2024年10月24日10txw1958

我知道解决方案离这里不远,但我正在努力实现根据用户输入为 map 比例分配数值的能力。代码很容易解释,但我希望用户从 4 种缩放状态中选择,使用输入,将数值返回到我的 map 作为缩放起点。

zoom=5 
while True: 
    where = raw_input('Where would you like a map of?') 
    try: 
        heatmap = folium.Map(location=geo(where), tiles='Stamen Toner', zoom_start=zoom) 
        pointmap = folium.Map(location=geo(where), tiles='Stamen Toner', zoom_start=zoom) 
    except AttributeError, GeocoderServiceError: 
        print "Cannot create map with the given location. Please try again." 
    else: 
        break 
while True: 
    zoom_state = ['city', 'state', 'country', 'world'] 
    zoom_state= raw_input('What level of detail would you like: city, state, country, or world?') 
    try: 
         for response in zoom_state: 
            if response is ['city']: 
                zoom == 9 
                if response is ['state']: 
                    zoom == 7 
                    if response is ['country']: 
                        zoom == 5 
                        if response is ['world']: 
                            zoom == 9  
    except TypeError, IndexError: 
        print 'Please enter: city, state, country, or world. Try again' 
        continue 
    else: 
        break 
 
heatmap = folium.Map(location=geo(where), tiles='Stamen Toner', zoom_start=zoom) 
pointmap = folium.Map(location=geo(where), tiles='Stamen Toner', zoom_start=zoom) 

就目前而言,我可以为 zoom_state 放入任何东西,它就会运行。

提前致谢!

请您参考如下方法:

您可能正在寻找这样的东西:

zoom=5 
while True: 
    where = raw_input('Where would you like a map of?') 
    try: 
        heatmap = folium.Map(location=geo(where), tiles='Stamen Toner', zoom_start=zoom) 
        pointmap = folium.Map(location=geo(where), tiles='Stamen Toner', zoom_start=zoom) 
    except AttributeError, GeocoderServiceError: 
        print "Cannot create map with the given location. Please try again." 
    else: 
        break 
while True: 
    zoom_level = {'city':9, 'state':7, 'country':5, 'world':9} 
    zoom_detail = raw_input('What level of detail would you like: city, state, country, or world?') 
    try: 
         zoom = zoom_level[str(zoom_detail)] 
    except TypeError, IndexError: 
        print 'Please enter: city, state, country, or world. Try again' 
        continue 
    else: 
        break 
 
heatmap = folium.Map(location=geo(where), tiles='Stamen Toner', zoom_start=zoom) 
pointmap = folium.Map(location=geo(where), tiles='Stamen Toner', zoom_start=zoom)