Practice Python Objects: Vowel Counter

I’m stuck on a particular exercise. The question asks to print the number of vowels in a list, which I believe I did. However, I’m getting an error stating that it Is incorrect.

Can someone provide some insight?


vowels = ["a", "e", "i", "o", "u"]
def vowel_counter(vowels):
  for letter in vowels:
    return vowels
Whats printed is 5

return inside a loop is always a good place to start. However, that is not the issue, as a whole.

When we read the name of the function we expect the function will count all the vowels in an input string, and return that count. Your function is iterating over a fixed list of vowels, then on the first iteration returning that same list.

Consider incorporating the vowels object within your function and then taking a string object as the argument.

def vowel_counter(string):
    count = 0
    for letter in string:
        if letter in 'aeiouAEIOU':
            count += 1
    return count

Now we can pass in any string, and get back what the function name suggests… A count of all the vowels.


Thank you very much for your help!

1 Like