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. 
Hope that’s helped. 