Else running, even though If returns True

So, I thought I had a good handle on if-else statements. I am doing an exercise from Python Crash Course by Eric Matthes and I’m running into this:

from pygal.maps.world import COUNTRIES

def get_country_code(country_name):
    '''Return the Pygal 2-digit country code for the given country.'''

    for code, name in COUNTRIES.items():
        if name == country_name:
            return code
        else:
            return None

print(get_country_code('Andorra'))
print(get_country_code('United Arab Emirates'))
print(get_country_code('Afghanistan'))

Which returns:

ad
None
None

When I run it in terminal with Python3.

When I run this:

def get_country_code(country_name):
    '''Return the Pygal 2-digit country code for the given country.'''

    for code, name in COUNTRIES.items():
        if name == country_name:
            return code


print(get_country_code('Andorra'))
print(get_country_code('United Arab Emirates'))
print(get_country_code('Afghanistan'))

I get:

ad
ae
af

When I don’t include the [else] statement, the [if] statement is definitely returning True.

Alternatively, I did get the code to work as expected when I did this:

def get_country_code(country_name):
    '''Return the Pygal 2-digit country code for the given country.'''

    names = []
    codes = []
    indices = []
    count = 0
    for code, name in COUNTRIES.items():
        if name == country_name.strip():
            names.append(name)
            indices.append(count)
            codes.append(code)
            count += 1

    for i in indices:
        if country_name in names:
            return codes[i]
        else:
            return None

And ran the same tests. I’m just confused why the else statement in the first code example would run at all. It even runs when I strip the white space from the comparing values.

I’m not a Python guy, but if I understand the logic with your original,

    for code, name in COUNTRIES.items():
        if name == country_name:
            return code
        else:
            return None

it is testing on the first iteration of the loop if it matches. I don’t think it ever makes it past the first iteration - it tests that first value and returns one or the other, so it only works if it matches on the first time.

Your other one works:

    for code, name in COUNTRIES.items():
        if name == country_name:
            return code

Because you are not bailing if there is a mismatch. If you want have a default return if there is no match, you shouldn’t be checking it every time, you should wait for the loop to be done and then return if nothing has been found.

Again, I don’t know Python, but that’s what I think I’m seeing - your algorithm is wrong.

1 Like

Thank you! I see where I went wrong now.