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.

3 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
1 Like

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