Fix `is_english` function

This is the function’s definition in the solution notebook:

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

How about another way around if we want to iterate to check the number associated with the character is lesser than 127 and return True first for english text then else false for character more than 127. I’m still not able to get the code right if we want to accept english text with less than 4 non-english characters. Please advise.

def is_english(string):
    non_ascii = 0
    
    for character in string:
        if ord(character) <= 127:
            non_ascii += 1
    
    if non_ascii < 4:
        return True
    else:
        return False

print(is_english('Docs To Go Free Office Suite'))
print(is_english('Instachat 😜😜😜'))

I don’t understand exactly what you’re trying to do.

One useful observation that I’ll make is that non_ascii is counting the number of ASCII characters, instead of non-ASCII characters. You might want to change something in your code.

1 Like