Reputation: 39
Writing a python function to iterate through strings. I want it to return False if any non-english characters are in the string
def check_if_english(app_name):
for character in app_name:
if ord(character) > 127:
return False
else:
return True
print(ord('😜')) # unicode value is 128540
check_if_english('Instachat 😜') #returns True
How is this ^ different than this:
def check_if_english(app_name):
for character in app_name:
if ord(character) > 127:
return False
return True
print(ord('😜')) # unicode value is 128540
check_if_english('Instachat 😜') #returns False
ord() documentation: https://python-reference.readthedocs.io/en/latest/docs/functions/ord.html
Upvotes: 1
Views: 335
Reputation: 21
The difference is that in the former code as soon as it encounters a correct english letter in a string it straight up returns true, without caring about whether the remaining substring contains a faulty letter
In the latter code, its doing what you actually want: check through all the letters in the string for false condition and only return true if all the letters in the string do not return false.
you can check the following example for more clarity:
#program to check if a binary string is all 1s
s="1000000"
s2="1111"
#wrong way- (returns true just by checking s[0])
def check_func_wrong(s):
for i in s:
if i==0:
return False
else:
return True
#correct way(checks the whole string)
def check_func_correct(s):
for i in s:
if i == "0":
return False
else:
continue
return True
print('s : ', s,' : ',check_func_wrong(s), 's2 : ', s2,' : ',check_func_wrong(s2) )
print('s : ', s,' : ',check_func_correct(s), 's2 : ', s2,' : ',check_func_correct(s2))
Upvotes: 1
Reputation: 332
Only the first executed return is enough to break out of the function.
The first one will check the first character "I" and execute the else part. Returns true when it reads "I".
The second one doesn't have an else. So nothing executes inside the for loop until the emoji is reached. When the emoji is reached, it returns False.
Upvotes: 1
Reputation: 71
The first function will return a result always evaluating the first character. The last func iterate over all the string characters and then, it will give you back a result.
Maybe you're looking for that function below. It will give you a dict with all string characters and its valuation.
def check_if_english(app_name):
return {character: False if ord(character) > 127 else True for character in app_name}
Upvotes: 1
Reputation: 326
The first program bases its answer solely on the first character. The first time through the loop it will always return either True or False.
The second program returns False if any character > 127. If it has run all the way through i,e, scanned all characters, and they're all ASCII then it returns True.
Note you're not testing for English you're testing for ASCII.
check_if_english('Das Boot')
would return True
Upvotes: 1
Reputation: 11228
in first one you are returning based on whether the first character follow the condition or not. and in 2nd one you checking all characters for the condition and after checking all character, returning True meaning condition not met
Upvotes: 2