There are a couple of mistakes. At first, alphabet always evaluated as "a" .
or in an ad simply means "if the previous thing is false, use it instead." Since "a" is true, it rests on its laurels. The rest of the letter was not even looked at by Python.
Next is any . any simply checks if something in iterable true. alphabet + alphabet + alphabet evaluates to "aaa" , so letter_word_3 always returns True .
When you check if "ice" == letter_word_3 'evaluates to "ice" == True .
To check if an arbitrary word is three letters, the easiest way is to use the following:
import re def is_three_letters(word): return bool(re.match(r"[a-zA-Z]{3}$", word))
Then you can use
is_three_letters("ice") # True is_three_letters("ICE") # True is_three_letters("four") # False is_three_letters("to") # False is_three_letters("111") # False (numbers not allowed)
To also allow numbers, use
import re def is_three_letters(word): return bool(re.match(r"[a-zA-Z\d]{3}$", word))
This will allow you to consider the material "h2o" as a three-digit word.
EDIT:
import re def is_three_letters(word): return bool(re.match(r"[az]{3}$", word))
The above code will only contain a lowercase letter (no numbers or capitals).
import re def is_three_letters(word): return bool(re.match(r"[az\d]{3}$", word))
This will allow you to use only lowercase letters and numbers (without capitals).
EDIT:
To check the number of letters by n, just change "{3}" to whatever length you want in the lines in the code above. eg.
import re def is_eight_letters(word): return bool(re.match(r"[a-zA-Z\d]{8}$", word))
The above will look for eight long words that will allow you to make capitals, lowercase letters and numbers.