'Two simple if-else codes in Python, should they not return the same value? Why each one returns something else

Two simple if-else codes in Python, should they not return the same value? Why each one returns something else.

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

# This returns True    
print(letter_check("strawberry", "a"))

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

# This returns False    
print(letter_check("strawberry", "a"))


Solution 1:[1]

They are not the same. The first def will return False only if none of the chars in word is equal to letter.

The second one checks only the first char in word and return True if they are equals or False if they aren't.

There is no need to loop by the way, use in keyword

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

Solution 2:[2]

When you call letter_check("strawberry", "a") the 2nd time, the function returns False because the first letter of strawberry is 's' and not 'a'.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Guy
Solution 2 Foussy