Return statements can be a source of tricky bugs in your programs. This article walks you through one such case. In the end, you will get a deeper understanding of how return statement works.
You are given the following list of strings:
Docs To Go™ Free Office Suite
These represent app names. The goal is to identify which of these strings are “non-English” names. Here’s how we decide which apps have non-English names:
If any of the characters is a non-English character, then the app’s name is not in English.
As a way to solve this problem, the course author suggested the following function:
def is_english(string): for character in string: if ord(character) > 127: return False return True
But Saif tried to solve this problem with the following, slightly different, function:
def saif_is_english(string): for character in string: if ord(character) > 127: return False else: return True
And he got stuck!
He found that for the names -
爱奇艺PPS -《欢乐颂2》电视剧热播 - his function returns accurate results, but for
Instachat 😜 and
Docs To Go™ Free Office Suite , it doesn’t seem to recognize the special characters and returns
What’s happening here??
Refresher on how
return statement works
return statement has the property that whenever the computer finds it, when it is running the function, it will quit the function right then and there and not do anything else.
We can read this from the official documentation :
returnleaves the current function call with the expression list (or
None) as return value.
Let’s test this. Below I’m defining a function that starts off immediately with a return statement, then it does some stuff and returns something else.
def a_func(n): return pow(n,2) # returns the square of n n=n+1 # reassigns n+1 to n return n # returns n (after reassignment)
Let’s try using it:
>>> print(a_func(1)) 1 >>> print(a_func(2)) 4 >>> print(a_func(3)) 9
So we see it’s always returning to us the square of the input. It ignores everything after the first return statement. Let’s now analyze the problem at hand.
Analyzing the Problem in our Case Study
Now that we know how
return statements work, let’s see them in action in the context of this question.
We’ll be analyzing the usage of
- First it enters the function with
- Then it will initiate a “for loop” over
characteris assgined the value of the first character in the input string, which means that
ifcondition is evaluated:
73, which is smaller than
127, the statement
ord(character) > 127is false and so we are sent to the
elsepart of the code.
- Once in
else, we hit the statement
return Trueand the function is exited right here.
So, as you can see, we ended up only ever looking at the first character of
That the function returns a correct result, is merely a consequence of the fact that the first character is “an English character”.
Let’s now see what happens with the usage of
Notice (in the right side of the animation) how we only get to a return statement after iterating over all the characters. Here ’s the link for this visualization.
I leave it to you to do this exercise for the other strings. I hope this clears it up.