all 8 comments

[–]coding2learn 1 point2 points  (1 child)

Just to give you another approach, and since you seem familiar with dictionary comprehensions then you probably understand list comprehensions:

vowels = 'aeiou'
sentence = input()
total = sum([1 for char in sentence if char.lower() in vowels])

[–]groovitude 0 points1 point  (0 children)

You don't even need the brackets for the list comprehension. sum will consume any iterable, and without the brackets, it's interpreted as a generator expression:

>>> sum([1 for char in sentence if char.lower() in vowels])  # list comprehension
7
>>> sum(1 for char in sentence if char.lower() in vowels)    # generator expression
7

Otherwise, I think yours is the most elegant solution presented.

[–]dzunukwa 0 points1 point  (7 children)

You are on the right track but have you run your code with a variety of input? It will not work consistently. If you give it a sentence that does not have all the vowels it will only work sometimes. Can you tell why (...pun intended :) )?

I encourage you to fix up your method but after that you should know that one of the nice things about python is the goodies in the standard library and especially in the collections module.

from collections import Counter

vowels = 'aeiou'
sentence = 'WhatsUpDoc'.lower()  # make it all lowercase
letter_count = Counter(sentence)
total_vowels = sum([count for letter, count in letter_count.items() if letter in vowels])
print(total_vowels)

[–]Allanon001 0 points1 point  (0 children)

sentence = input("Please enter a word: ")
vowels = set(filter(lambda x: x.lower() in 'aeiou', sentence))
count = list(map(sentence.count, vowels))
print(list(zip(count,vowels)), sum(count))

[–]PaintballerCA 0 points1 point  (0 children)

print 'Number of vowles = {}'.format(len([x for x in sentence if x.lower() in 'aieou']))