Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python function to detect in vowel in word

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".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading