简体   繁体   中英

How to error handle properly python

I know the solution is not far off here but I am struggling to implement the ability to assign a numeric value to map scale based on user input. The code is pretty self explanatory but I would like the user to pick from 4 zoom states, using the input, return a numeric value to my map as the zoom start point.

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)

As it stands now I can put anything in for zoom_state and it will run.

Thanks in advance!

You might be looking for something like this:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM