Return and else: inclusion in an if statement

Why are these three functions not the same, when I include the else: I always get false.
Why does the false return sit out of the if statement or underneath it without an else?

 def letter_check(word,letter):
   for i in word:
     if i == letter:
      return True
      return False
````Preformatted text`
    
print(letter_check("strawberry","a"))

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

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

True
True 
False

Edit: The false statement would be separate to the code block because it’s not supposed to be part of that loop to check for a specific letter. Remember that return would exit the function once it is executed, what then would happen if return True was never reached? Does this match the intention of the function? Try following the flow with pen and paper, in your head or even with print statements if you prefer.

Original

It’s hard to tell without indentation please try wrapping the code with backticks (or use </> button to do it for you)-
```
codegoeshere
```

2 Likes

Hi that kind of makes sense and Ill try with the paper? Why does adding an explicit else in the last one break though?

The statement is true so the function should return true?

Thanks

Worked it out, thanks for the tips!