如何正确处理python中的错误

如何正确处理python中的错误,python,error-handling,folium,Python,Error Handling,Folium,我知道解决方案离这里不远了,但我正在努力实现基于用户输入为地图比例分配数值的功能。代码非常简单,但我希望用户使用输入从4种缩放状态中选择一个数值作为缩放起点返回到我的地图 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_sta

我知道解决方案离这里不远了,但我正在努力实现基于用户输入为地图比例分配数值的功能。代码非常简单,但我希望用户使用输入从4种缩放状态中选择一个数值作为缩放起点返回到我的地图

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_状态输入任何内容,它将运行


提前谢谢

您可能正在寻找以下内容:

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)

首先,您对细节级别的原始输入是对zoom_状态列表的过度写入。运行时的输出是什么?此代码有很多问题。首先,定义缩放状态,然后对其进行修改。其次,if语句的嵌套是可疑的。第三,你在zoom_状态上循环(出于某种原因),但是,你从来没有在循环中使用响应变量。谢谢你们,显然我对这个(第一季度)是新手。输出结果是,它的默认值为5。我将改变zoom_状态,希望它能正常工作除了答案的贡献,你可能想看看,来解决你还没有注意到的错误,除了TypeError,Indexer这正是我要找的我真的很感谢你的帮助。抱歉把这里弄得一团糟!:)没问题!每个人都必须在某个地方学习。你能看到我用缩放级别做了什么吗?我把它变成了一个字典,所以查找zoom_detail关键字会返回与该名称成对的值。它简化了if语句,并且相当类似于python。它比我想象的要优雅得多。再次感谢你,这非常有帮助!