lundi 2 septembre 2019

if statement within For loop - logic error

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.

I don't understand why the else clause doesn't work here.

# My Code (logic doesn't seem to work)

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:’))

#Solution Code (works but I don't understand why)

def is_english(string):

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

    return True

The following should be the expected output for MY CODE: True False False False

However, the actual output is: True False True True

Aucun commentaire:

Enregistrer un commentaire