random_characters = "adak"
for letter in random_characters:
print(letter)
if letter == "a":
print("Vowel")
…emits as output:
a
Vowel
d
a
Vowel
k
If I only include one letter in the if statement the code runs fine. However, If I add one more through an or statement…
a
Vowel
d
Vowel
a
Vowel
k
Vowel
"Vowel" is printed after every iteration.
>Solution :
I’m going to read between the lines and guess that you have made the very common mistake of writing:
if letter == "a" or "e":
because that would produce the results you see. The reason is, that is interpreted as
if (letter == "a") or "e":
and since "e" is always True, the if is always taken. If you want to compare to multiple letters, use in:
if letter in ("a","e","i","o","u"):
although in this case, you could also write:
if letter in "aeiuo":