Language Checking Function not Working Properly

Screen Link:

My Code:

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

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

What I expected to happen:

What actually happened:


This is a common problem with this exercise. Rather than tell you explicitly how to fix it, I will try to explain exactly what your function is doing and hopefully you can see how to fix it yourself. Let me know what you think.

So, we start with a for loop that begins with a conditional statement that checks if the character is greater than 127. If that first character is greater than 127, the function should return False. If the first character is not greater than 127 it should immediately return True. In other words, the way the function is written presently, it never gets beyond testing that first character because the function runs into a return statement and will immediately exit.

This explains why only the second test is failing ('爱奇艺PPS -《欢乐颂2》电视剧热播') because it’s the only example that has a leading non-English character.

Can you see how you can make a small modification to your function so that it checks all the characters and not just the first one?

HINT: you do not need to add any code to your function to get it to work the way you want it to.

1 Like

I put the return True statement outside of the for loop and it worked, thanks!