Omitting else statements

Hey everyone, first time trying these forums so I’ll give it a go.

Just a bit confused with lesson 10 in the strings and conditionals excersise. The following answer was

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

I would have thought that after: return true, you would follow with: else: return false.

I looked back into control flow and couldn’t find that info, but I could be wrong.

Anyone else find this frustrating?

Thanks for your time.

Hi there.

You don’t need to have an else statement in a conditional flow structure, and indeed there are many instances - including this one - where doing so would not result in the desired outcome.

Let’s assume we had written the code in the way you were expecting, so we had this:

def letter_check(word, letter): for character in word: if character == letter: return True else: return False result = letter_check("python", "o") print("Is the letter 'o' in 'python'?: {}".format(result))

If we run the codebyte above, you’ll notice that we get the result False - but we can clearly see from looking that the letter o is certainly in the word python.

This function no longer works correctly, because we’re now only checking the first letter. In this example, we’re testing whether the letter o is the same as the letter p - the first iteration of our for loop - and discovering that they’re not the same. Since we have an else block in our conditional flow, Python runs it and immediately returns False.

Without the else block, Python will continue to review the rest of the letters. If it finds a match, which is to say if the if condition is met, then we run that block of code and return True. Otherwise, Python gets to the end of the loop and continues with the rest of the function to return False.

One thing which I would encourage you to do, as much as possible, if you’re struggling to follow conditional flows or loop iterations is to write out to the terminal. The print() function is one of your greatest allies in seeing precisely what your program is doing as it goes along. Here’s an example, with both implementations:

def letter_check(word, letter): print(f"Checking for letter '{letter}' in word '{word}'") for character in word: print(f"FOR loop -- testing letter '{character}' from word!") if character == letter: print(f"MATCH! Character '{character}' is same as target letter '{letter}'!\n\n") return True print("No match, looping again!") print("No matches found!! Exiting function!\n\n") return False def letter_check_else(word, letter): print(f"Checking for letter '{letter}' in word '{word}'") for character in word: print(f"FOR loop -- testing letter '{character}' from word!") if character == letter: print(f"MATCH! Character '{character}' is same as target letter '{letter}'!\n\n") return True else: print("ELSE: No match, return FALSE!") return False print("No match, looping again!") print("No matches found!! Exiting function!\n\n") return False print("------ Example 1 ------") result = letter_check("python", "o") print("1) Is the letter 'o' in 'python'?: {}\n".format(result)) print("------ Example 2 ------") result2 = letter_check_else("python", "o") print("2) Is the letter 'o' in 'python'?: {}\n".format(result2)) print("------ Example 3 ------") result3 = letter_check("python", "c") print("3) Is the letter 'o' in 'python'?: {}\n".format(result3))

I hope that’s helped illustrate why you don’t necessarily want to have an else condition every time you’re using conditional flow structures. It’s important to consider precisely what you’re testing for, as otherwise you may inadvertently prevent your program from doing what you intended it to do. :slight_smile:

Hope that’s helped. :slight_smile:

3 Likes

In addition to @thepitycoder’s excellent answer, i would like to encourage you to always think about the “Zen of Python”.

In many cases, just like the one you described, there is no need for an if-statement at all. Consider the following code:

def letter_check(word, letter):
  return letter in word

The above code does the exact same thing as the code below, however the readability is way better:

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

I know this is not really an answer to your question, but i do hope it will give some food for thought!

1 Like

Thanks for the explanation! I see now that the code immediately goes to else if the first characters don’t match. I’ll try printing out what I’m doing as I go along as well.

As for placing two returns one after the other, that must have been a gap in my knowledge.

Thanks again.

Yes I shouldv’e simply done the following:

def letter_check(word,letter): if letter in word: return True return False result = letter_check("python","5") print(result)

I’ll keep that in mind for next time. Thanks