I wrote this python code to detect if one of the vowel is a word then print an output; but if it is not working as I expect. It always print "Contains a lowercase vowel" . I have try many way and it doesn’t work. Any help will be appreciated. FYI; I am learning python. Thanks!
def contains_vowel(word):
word = str(word)
if "a" or "e" or "i" or "o" or "u" in word:
return word +" = " + "Contains a lowercase vowel."
else:
return word +" = " + "Doesn't contains a lowercase vowel."
print(contains_vowel("turtle"))
print(contains_vowel("sky"))
I try changing up the code. I did a for loop that loop trough each character and print an output; but that isn’t really what I want. If any of the character is in the word it should print "Contains a lowercase vowel".
>Solution :
You can check for vowel in the following function :
def has_vowel(word):
vowels = "aeiouAEIOU"
return any(char in vowels for char in word)
# Example usage:
word_to_check = "example"
if has_vowel(word_to_check):
print(f'The word "{word_to_check}" contains a vowel.')
else:
print(f'The word "{word_to_check}" does not contain a vowel.')
Refer the working code sample here