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