428-6 Python Course 1 Project - removing non-english apps

I have a question regarding my function to filter for characters > 127, which is the same as the solutions.

The link to the problem.

My code is:

def english_check(string):
     for character in string:
          if ord(character) > 127:
               return False
          else:
               return True

    
print(english_check('Instagram'))
print(english_check('爱奇艺PPS -《欢乐颂2》电视剧热播'))
print(english_check('Docs To Go™ Free Office Suite'))
print(english_check('Instachat 😜'))

However, not sure why, but for the string ‘Instachat :stuck_out_tongue_winking_eye:’ and ‘Docs To Go™ Free Office Suite’, it actually returns True.

And when I check the individual characters with the ord() function, it, of course, returns characters > 127.

string = 'Instachat 😜'

for character in string:
    if ord(character) > 127:
        print('one or more character is above 127')
        print(character)

Err, doesn’t seem right.

1 Like

Hey, Ajtam.

What’s below was copy and pasted from another topic asking the same question.

Technical Preamble

The 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:

return leaves 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

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 print(ajtam_is_english('Instagram')), where saif_is_english is the function english_check in your code.

  1. First it enters the function with Instagram as its input.
  2. Then it will initiate a “for loop” over Instagram.
    1. character is assgined the value of the first character in the input string, which means that character is assigned I.
    2. The if condition is evaluated:
      • Since ord(character) is 73, which is smaller than 127, the statement ord(character) > 127 is false and so we are sent to the else part of the code.
    3. Once in else, we hit the statement return True and the function is exited right here.

So, as you can see, we ended up only ever looking at the first character of Instagram. We can even use Python Tutor’s Visualizer to see this:

saif_is_english

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 print(is_english('Instagram')):

is_english

Notice (in the right side of the animation) how we only get to a return statement after iterating over all the characters. Here is the link for this visualization.

I hope this helps.

2 Likes

Ah, I see. Being that I used IF and ELSE in the same loop, it checks both criteria and exited the loop on the ELSE because first letter was True.

But with the loop with only IF, it continued to loop and test/check all characters in the string to see if each character was > 127.

Makes sense now. Thanks!

1 Like

Exactly!