-2

How I write a function "noVowel" in python that determines whether a word has no vowels?

In my case, "y" is not a vowel.

For example, I want the function to return True if the word is something like "My" and to return false if the word is something like "banana".

  • 3
    Possible duplicate of [Detecting Vowels vs Consonants In Python](http://stackoverflow.com/questions/20226110/detecting-vowels-vs-consonants-in-python) – idjaw Sep 23 '16 at 01:16

2 Answers2

1
any(vowel in word for vowel in 'aeiou')

Where word is the word you're searching.

Broken down:

any returns True if any of the values it checks are True returns False otherwise

for vowel in 'aeiou' sets the value of vowel to a, then e, then i, etc.

vowel in word checks if the string word contains vowel.

If you don't understand why this works, I suggest you look up generator expressions, they are a very valuable tool.

EDIT

Oops, this returns True if there is a vowel and False otherwise. To do it the other way, you could

all(vowel not in word for vowel in 'aeiou')

or

not any(vowel in word for vowel in 'aeiou')
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
0

Try this:

def noVowel(word):
    vowels = 'aeiou' ## defining the vowels in the English alphabet
    hasVowel= False ## Boolean variable that tells us if there is any vowel
    for i in range(0,len(word)): ## Iterate through the word
        if word[i] in vowels: ## If the char at the current index is a vowel, break out of the loop
            hasVowel = True
            break
        else: ## if not, keep the boolean false
            hasVowel=False
    ## check the boolean, and return accordingly
    if hasVowel: 
        return False
    else:
        return True

Hope it helps!

codemirel
  • 160
  • 10