Logic error - not always giving the expected result

https://app.dataquest.io/m/350/guided-project%3A-profitable-app-profiles-for-the-app-store-and-google-play-markets/6/removing-non-english-apps-part-one

Your Code: ‘def check_string(string):
for char in string:
if ord(char) <= 127:
return True
else:
return False’

Error: ‘I notice a Logic error. it is not giving the expected result in some cases.like in this case where the string was’Docs To Go™ Free Office Suite’ ’
Question:I checked the solution but I don’t understand and what is the problem with my code for not bringing the required result. it was suppose to return false in that specific case I cited but it still gives true which is wrong’

1 Like

Hi @adeyemiqudus361,
I thank your code will return True the first time you find an English character, and when you run the test for Docs To Go™ Free Office Suite you well get True instead of False because of D is an English character the condition if ord('D') <= 127 is True and the function will return True.
You can fixe the problem by

def check_string(string):
    for char in string:
        if ord(char) > 127:
            return False
        else:
            return True
1 Like

please can you explain to me how it continues the iteration in this case and why it stops at the first letter in my case

In the first case, the code will return True the first time you find an English character and having an English character doesn’t mean that word is in english, in the example of ’Docs To Go™ Free Office Suite’ you will stop iterating after the first iteration and you will get True because you find english character (D)
By opposite you can say that the word isn’t in english if you just find one non-english character, in second case you will continue iteration until you find an non-english character , if you didn’t find any one you can say that the word is in english, for same example you will continue iterating until you get to ‘™’ and you stop iterating (you can use the function print to see why) and the function will return ‘False’

for i in '™':
   print(ord(i))

226
132
162

Hi and welcome to the forums @adeyemiqudus361!

The issue you’re running into is that a function halts its execution the instant it runs into a return clause.

Another learner had this exact question here which I provided an answer to some time ago!