Vowel counting

Can someone tell me what is wrong with this script. I am python newb, but I cannot figure out what might cause it to not function.

def find_vowels(sentence): """ >>> find_vowels(test) 1 """ count = 0 vowels = "aeiuoAEIOU" for letter in sentence: if letter in vowels: count += 1 print count if __name__ == '__main__': import doctest doctest.testmod() 
+7
python
source share
7 answers

Besides the fact that you are returning the score but expecting a vowel string, as others have said, you must also change the string

 >>> find_vowels(test) 

to

 >>> find_vowels('test') 

You forgot the quotes!

+7
source share

You print count (number), but your test expects the letter e .

In addition, the greater the Pythonic method of counting vowels will be an understanding of the list :

 >>> len([letter for letter in 'test' if letter in vowels]) 1 

Want to see the vowels you found? Just omit this leading len function:

 >>> [letter for letter in 'stackoverflow' if letter in vowels] ['a', 'o', 'e', 'o'] 
+8
source share

Your test expects the function to print the found vowels, but instead you print a counter. You also pass it the test variable instead of the string 'test' , you need to do

 >>> find_vowels('test') 

Finally, the indentation is disabled, but I assume it was an insertion issue

+1
source share
 def count_vowels(word): ''' str->(number) returns the number of vowels used count_vowels('ramesh') >>>2 count_vowels('sindhu') >>>2 ''' vowels=list('a,e,i,o,u') return vowels.count(word) 
+1
source share

You need to separate the body of find_vowels.

0
source share

Why not use str.count () for example s = 'heeello world'

print str.count (s, 'e') 3

0
source share

This solution is fairly clean and more efficient than solutions already sent for large strings; all the hard work happens in the C-loop of building the Counter object.

 from collections import Counter vowels = "aeiuoAEIOU" def count_vowels(txt): c = Counter(txt) return sum(c[v] for v in vowels) print count_vowels('testing') 
0
source share

All Articles