Alternative to is_english function

To determine if a string is English or not. The original code in the course is below, and it does not work right for the emoji and TM…

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('Instachat 😜'))
print(is_english('Docs To Go™ Free Office Suite'))

The above code returns:
True
False
False
False

So the recommended fix is to add a check to only remove i there are more than three characters outside of ASCII range. That’s all fine and good. But why can’t I just do this (indent the return True) which seems to work…

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('Instachat 😜'))
print(is_english('Docs To Go™ Free Office Suite'))

So the code in the solutions first has you write the function without the else, and this does not work. But the fix is to add a counter and more if statements, when just adding in the else seems to work.

What actually happened:

Paste output/error here

hi @davidriasp

Welcome to DataQuest Community!

I don’t see anything different in the two codes. Could you please elaborate further on this. Perhaps attach the notebook you are working, for us to help you better.

hey @davidriasp

better yet. hope this post might help you.

1 Like