P4e chap 13; handling error for country code for disputed territories

Hi,

Doing this excellent course ‘Python for Everybody’, exercize in Chapter 13 asks to modify this file https://www.py4e.com/code3/geojson.py to show country code for the city entered by user.

Country code is contained in the last element of the list ‘address_components’ in the json file returned from googlemaps api. Except when the city entered is in a disputed territory (like Sevastopol for instance) In which case ‘address_components’ only has 1 element, which doesn’t include a country code, so nothing gets returned.

You can see the json file by running the unmodified code linked to above.

I have tried to write code to return the length of the list ‘address_components’ , and to print an error message if its value is less than 2.:

add_comps = js['results'][0]['address_components']
If len(add_comps) > 2:
        c_code = js['results'][0]['address_components'][-1]['short_name']   # always last one though, so index = [-1]
        print(c_code)
print('The lack of a country code suggests that this location exists within a disputed territory')

But this fails to compile. Am I taking the wrong approach to this part of the problem, or have I got a syntax problem somewhere that I can’t see?

Apologies ahead of time if I’ve formatted the code wrong.

How so? You get like an error or what?
Also Python doesn’t exactly “compile” code, but runs an interpreter.

Ok, I got the wrong terminology. I have to admit I didn’t remember the error code; it was long, and didn’t tell me anything I could understand, so I took another look this morning; on looking at more json files returned by the original py4e code, I realised that the number of elements in ‘address_components’ varies widely, as does their contents, so I scrapped my code that looks only at the last element, and rewrote from scratch:

    undisputed = False 
    for nt in range(0, len(js['results'][0]['address_components'])):       
        if 'country' in js['results'][0]['address_components'][nt]['types']:   
            c_code = js['results'][0]['address_components'][nt]['short_name']   
            print(c_code)
            undisputed = True
    if not undisputed: print('Lack of a country code suggests that this location exists within a disputed territory')

The country code is always available if ‘country’ is present in a list element, so I looked at all list elements, and only returned the value of ‘short_name’ in that element. If ‘country’ isn’t there, (ie c_code isn’t there) it gives a helpful message instead. It works now.

Should I close this thread? Thanks

Next time, copy the error message as Python is usually offering a lengthy Traceback-error, listing all files the interpreter was working on, while encountering the error. It’s hard to understand at first because it’s usually 90% stuff from other libraries which doesn’t tell you a lot. But we could look at it and tell you the 10% important stuff (it’s usually the top showing the line of your code and the bottom listing the error-type).

Anyway, if you solved it, just mark your post as the solution of the topic :wink:

Thanks, sounds like good advice, will do!