Help with code to count number of vowels in a word?

I was practicing writing code in Edabit and one of the challenges was to write code that could count the number of vowels in a word. The Codebyte below is the code that I tried (keyword: tried) to write, but nothing gets output. Not even an error. Could anyone tell me what is wrong with this code? I know there are probably way easier ways to write a code that is able to do this, but I’m only like two weeks deep into learning how to code.

def count_vowels(txt): number_of_vowels = 0 for letter in txt: if letter == 'a' or 'e' or 'i' or 'o' or 'u': number_of_vowels += 1 continue count_vowels('Codecademy')

At present you don’t have any route to output information, make sure to tell the interpreter what you want to happen. Your function doesn’t return and nothing is printed to the console either.


A couple of things. Firstly, the reason you’re not seeing any output is because there’s no output from the function! You need to either return from the function number_of_vowels and then print the function call or print it inside the function.

In addition, your if statement isn’t quite right. you can’t chain equality the way you have. Essentially what you have written is

if letter == 'a' or 'e' == 'e' or 'i' == 'i' or 'o' == 'o' or 'u' == 'u'

and the latter 4 are always true, therefore it always counts. You need to either do letter == vowel for every vowel, or you can use the in keyword with an array like so:

if letter in ['a', 'e', 'i', 'o', 'u']:

You should find it works after that.


It was my or statements! I do not think I have learned about arrays yet. Time to do some googling. Thank you!

1 Like

In Python they are called lists. If they have not yet come up, then do not use that solution. Use what you know.

Have you learned about the in keyword, yet?

1 Like

Oh, then I have learned about them! I have learned about the in keyword. I think I did not know that lists could do more than being assigned to a variable. Now I know that they can also be used in loops to search for a certain item!

And so can strings.

for vowel in 'aeiouAEIOU':

One string, instead of many.

1 Like