How does the indentation level of `return False` change the code?

Hello, this exercise has highlighted something that I haven’t exactly understood about Python programming, but up until this point I couldn’t put my finger on it… I’m having trouble grasping how python interprets indentation levels. In the following example:

def letter_check(word, letter):
_for character in word:
__if character == letter:
___return True
_return False

The code returns True if the letter is found in the word. All good so far.

If I put a single space in front of ‘return False’, the program throws an indentation error. Not sure why it has to see everything spaced twice, but not my issue here. If another space is added, yielding:

def letter_check(word, letter):
_for character in word:
__if character == letter:
___return True
__return False

the program now returns ‘False’ with the same input. With another two spaces before ‘return False’, i.e.:

def letter_check(word, letter):
_for character in word:
__if character == letter:
___return True
___return False

The program now returns ‘True’ again with the same input. I guess what I’m asking is how should my brain be interpreting the indentation levels here? I should probably straighten this out before things get much more complex…

Thanks in advance,
Joe

edit: replaced spaces with underscores to highlight issue

Hi,

First, if you </> for Preformatted text it makes it easier to read:

def letter_check(word, letter):
  for character in word:
    if character == letter:
      return True
  return False

In your second code it will only return True if the first letter of the word is the letter. Else it will continue the loop. If character == letter is not True so it continues to your next line, return False.

Your last code is only working correctly if the letter is in the word. You will never get False because at the moment the if statement is True it will go to Return True. If the statement is false it will go back to the for loop. So there is no Return statement if the letter is not in the word and it will print out “None”.

I hope this helped!

Edit: If you put in the code in here you can visulize it and you see the difference in the different Return False placement.

17 Likes

Hi Groentekroket,

Thanks a lot for sharing the website for visualizing the code. It is extremely helpful. I was having trouble with loops and lists but this helps a lot.

This was my original code and I was wondering why it wasn’t working and luckily I removed the else: and just used return false (randomly moved it around to try to get the answer)

but the visualization website really helps me see that using else: means the if statement will only look at the first letter of strawberry to see if it is a match and if that first letter is not then go directly to else:
thanks a lot

def letter_check(word, letter):
  for alphabet in word:
    if alphabet == letter:
      return True
    else:
      return False
2 Likes

Hey! Thank you for Visualize Python execution code link!
Turns out to be extremely helpful when you’re doing your first steps)

2 Likes

Hello there,

Thank you for your answer.
I still do not understand the code. To test out my logic, I changed it as follows:

def letter_check(word,letter):
for i in word:
if i==letter: print(‘a’)
print(‘b’)

print(letter_check(‘strawberry’,‘a’))

I wanted to know whether I’d have both a and b printed out, which I did. I also got a None.

My logic is as follow: s is not a, move on. t is not a, move on… a is a, print a, get out of the loop and print b.

Therefore:

def letter_check(word,letter):
for i in word:
if i==letter: return True
return False

print(letter_check(‘strawberry’,‘a’))

Should return:

  1. True
  2. False

But it doesn’t. It works fine, which I don’t understand.
While the following piece of code:

def letter_check(word,letter):
for i in word:
if i==letter: return True
else: return False

print(letter_check(‘strawberry’,‘a’))

Doesn’t work, while I think it should. Because s is not a, move on, t is not a, move on. a is a, True. Out of the if block.
However, it doesn’t behave this way.

Help, people?!
Lol

Best,
Kathy

Hi @katyagourgue08710287, welcome to the forums. I’ll try and answer here but please see How do I format code in my posts? for future posts if you’re adding code since the lack of indentation makes this very hard to understand.

There’s a big difference between print and return, print is a function with a side effect of outputting strings (typically to the console) there’s nothing particularly special about it. On the other hand return is a statement that can only be used inside functions, it evaluates the expression on the right hand side, returns that value to the original caller and exits the function. So if any return statement is executed in your function, no further lines will be executed because you leave the function at that point.

def func():
    return "finished"
    # these lines below will never be reached!
    x = 4
    print(x)

This is why your first function (with two prints) prints twice, why the second only returns once (where you then print the returned value) and why the final one does not work as intended (since it can return False, exiting the function much earlier than you expected).

The final bit you noted about your first function outputting None is that functions always return something. By default if a function runs to the end without returning anything it will return the None object instead. Since you then print the output of this function, it prints None in addition to the prints executed from inside the function.

Most functions use return at some point because they’re more flexible, you can store or use the returned value as well as print it (though that isn’t always the case).

1 Like