Function 'is_english' is right but is wrong

Screen Link:


My Code:
def is_english(string):
    for c in string:
        if ord(c)<127:
            return True
        elif ord(c)>127:
            return False
print(is_english('Instagram'))
print(is_english('爱奇艺PPS -《欢乐颂2》电视剧热播'))
print(is_english('Docs To Go™ Free Office Suite'))
print(is_english('Instachat 😜'))

What I expected to happen:

True
False
False
False

What actually happened:

True
False
True
True

I have started the next exercise but I realized that the string with characters ‘™’ and the emoticon are True, so I don´t really get where I did wrong, even the answer is right…

Thank you

Hi @javiermarzalb:

If I’m not wrong, ™ and the emoji are non-ascii characters so you cannot use ord() on them. To mitigate this, you can simply return False in the function if the number of non ascii characters exceeds 3 as suggested in the sample solution. As suggested in this article, the emoji may take up a certain number of bytes depending on the encoding type (e.g. utf-8 etc.) Based on that you can decide on the criteria/threshold on how many allowable unicode characters you want.

1 Like

Hi Javier, I think there is a different problem with the code.
If a character is < 127, your function returns True, AND THAT EXITS THE FUNCTION. Meaning, it doesn’t go through any of the other characters in the string.
I tried a similar code but still got stuck. I removed the first return statement, so it returns False if a letter is > 127, and has no return if it is <= 127. At the end I put return True, so if nothing is > 127 it returns True.
def is_eng(string):
for item in string:
if ord(item) > 127:
return False
return True
print(is_eng(‘Instagram’))
print(is_eng(‘爱奇艺PPS -《欢乐颂2》电视剧热播’))
print(is_eng(‘Docs To Go™ Free Office Suite’))
print(is_eng(‘Instachat :stuck_out_tongue_winking_eye:’))

The only problem is it still doesn’t detect the emoji and the tm.
Hope that makes sense, sory I didn’t figure out how to format the code better.

I corrected the indentation and it works. Thanks to netanel I noticed why I met the same problem with Javier
def eng_char(string):
>for char in string:
>>if ord(char) > 127:
>>>return False
>return True

I had the same problem and I’ve managed to fix it, with the help of @dungdidom’s comment above, but it took me a bit of thinking it through to understand why, so thought I’d share:

When you write the function as I had, which was

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

It loops through the string, returning False the first time it comes across a non English character (>127). Trouble is, as soon as it finds an English character it goes to the next line. If that’s an elif ord(character)<= 127, it will pass the second ‘if’ criteria and return True or if it’s an else, like mine, it’ll just return True because it was told to.
Either way, this exits the loop and ends the function as it has come to a ‘return’, skipping any other characters than come after.
That’s why it returns False for ‘爱奇艺PPS -《欢乐颂2》电视剧热播’, where the first character is non_English, but doesn’t work for the other two cases where there’s an English character before the emojis.

This, on the other hand, runs through all the characters until it finds an ord > 127. Only if it doesn’t will it move on to outside of the loop and perform the second return.

def english_characters(string):
        for character in string:
            if ord(character) > 127:
                return False
        return True
1 Like