Return Placement in Functions

Hi, I noticed depending where I put the Return True in this exercise, Guided Proj 1: 6 Removing Non English Apps Part 1, it significantly changes the answers the system returns. Could someone explain please? Below is my code.

def checkenglish(word): 
    for varbl in word:
        if ord(varbl) > 127:
            return False
    return True #placement of the second RETURN is crucial and can screw up the results 



checkmeout1=checkenglish(word='Instagram')
print(checkmeout1)
checkmeout2=checkenglish(word='爱奇艺PPS -《欢乐颂2》电视剧热播')
print(checkmeout2)
checkmeout3=checkenglish(word='Docs To Go™ Free Office Suite')
print(checkmeout3)
checkmeout4=checkenglish(word='Instachat 😜')
print(checkmeout4)

The indentation of return True tells python what to check, hence the results differ.

If you do:

def is_english(string):
    
    for character in string:
        if ord(character) > 127:
            return False
    return True

print(is_english('Instagram'))
print(is_english('爱奇艺PPS -《欢乐颂2》电视剧热播'))
print(is_english('Docs To Go™ Free Office Suite'))
print(is_english('Instachat 😜'))

True
False
False
False

You see how return is lined up with for? This means the return statement wont give a result until it checked all of the characters in the string.

Below return is moved one position inwards, this means it checks the first character not the whole string. I added a print statement so you can see what it checks.

def is_english(string):
    
    for character in string:
        if ord(character) > 127:
            return False
        print(character)
        return True

print(is_english('Instagram'))
print(is_english('爱奇艺PPS -《欢乐颂2》电视剧热播'))
print(is_english('Docs To Go™ Free Office Suite'))
print(is_english('Instachat 😜'))

I
True
False
D
True
I
True

Here it returns if the first character is within 127 or no.

See also this topic for a detailed explanation of how this function works.

Solid. Thanks gentlemen.