Can anyone please tell me what is wrong with this script. I am a python newb but i cant seem to figure out what might be causing it not to 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()
You're printing count
(a number), but your test expects the letter e
.
Also, the more Pythonic way to count the vowels would be a list comprehension:
>>> len([letter for letter in 'test' if letter in vowels])
1
Want to see the vowels you've found? Just drop that leading len
function:
>>> [letter for letter in 'stackoverflow' if letter in vowels]
['a', 'o', 'e', 'o']
Besides the fact that you're returning a count but expecting a string of vowels, as others have said, you must also change the line
>>> find_vowels(test)
to
>>> find_vowels('test')
You forgot the quotes!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With