If - else error in python

#Can someone help me figure out why this code is not returning False for ‘Instachat :stuck_out_tongue_winking_eye:’ and ‘Docs To Go™ Free Office Suite’? They contain characters (emoji and TM respectively ) whose unicodes are greater than 127, so technically, it should return False for both of them.

def is_english(string):
for character in string:
if ord(character) > 127:
return False
else:
return True
print(is_english(‘Instagram’))
print(is_english(‘爱奇艺PPS -《欢乐颂2》电视剧热播’))
print(is_english(‘Docs To Go™ Free Office Suite’))
print(is_english(‘Instachat :stuck_out_tongue_winking_eye:’))
print(ord(‘:stuck_out_tongue_winking_eye:’))

Hi there @neeyatiajmera, welcome to the community.

For the sake of readability, it’s good practice to envelop any code you copy-past with 3 back ticks. So ```abc```, if enclosed in 3 back-ticks, will appear as a coded block: abc

This is formatting you will also find yourself using when asking questions on sites like stackoverflow.

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

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

I think your code should work if you just get rid of the else clause, and make the return True part the “default”, as it were, unless there happens to be a char with unicode greater than 127. So you’ll take the return True out of the for loop like so:

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

Run this new function on your strings, and it should work.

I have a vague idea as to why that’s the case, but I don’t know if I’d be able to articulate it sufficiently, so I’ll let someone else take it from here!!

2 Likes

Hello, thank you so much for your suggestion. I’m still a newbie so I’m still in the process of figuring things out. I still don’t understand as the why the code with the else statement doesn’t work. Is it because it is only taking into consideration the last character of the string? Could you please clarify?

I think this is what’s going on:

A function ends its execution the moment it hits a return clause. The way your for loop was initially written, after the very first round of iteration, it already hit a return clause, since it was either going to return True, or return False.

I don’t think it’s taking into consideration only the last character. Instead, the problem here is the opposite - it’s only taking into consideration the first! You can test this on other strings to confirm whether this is what’s going on.

In comparison, with the alternative code I proposed, you can see that because the return True is outside the for loop, it keeps iterating over and over again until it either finds a unicode above 127, or it doesn’t find one and therefore returns a True value.

This is just educated speculation because I’m a learner myself! Hopefully someone else can confirm it for us :slight_smile:

2 Likes

Yes!!! You are so right! I tested it by deliberately putting emojis in the front and indeed it is only taking into consideration the first character of the string. I definitely learnt something about return statements today.

Thank you so so much! Your explanation was spot on and it really helped me. Thank you for being there for newbies like me!

1 Like

Glad I could clarify that! It was my pleasure :blush:. I remember being thrown off by it a few times myself until I gathered what was going on!!

If it helped solve your issue, do you think you could mark the answering post as the solution using the “solved” feature? That helps other learners find the answer quicker!

1 Like