This is some code I have made to count the most occuring vowels within the inputted message. Everytime I run it, it prints out it out 5 times, how can I fix this?

I also want it to display a message when there is no vowel entered. Maybe someone can point me in the right direction?

msg = input("Enter a sentence: ")
    v = set(['a', 'e', 'i', 'o', 'u'])
    a = dict()
    for letter in v:
        a[letter] = msg.count(letter)
    for letter in a:
        m = max([ (a[v],v) for v in a])
        print("Most occuring vowel is", m)
4 Years
Discussion Span
Last Post by vegaseat

I also want it to display a message when there is no vowel entered

Check the length of a set intersection

test_sentence="The quick brown fox"
v = set(['a', 'e', 'i', 'o', 'u'])
print v.intersection(set(test_sentence))

or set some indicator

msg = input("Enter a sentence: ")
#v = set(['a', 'e', 'i', 'o', 'u'])
a = dict()

for letter in "aeiou":
    a[letter] = msg.count(letter)
    if a[letter} > 0:
        vowel_found = True

Edited by woooee


sorry woooee: set drops multiple vowels, doesn't it?

>>> test_sentence="The quick brown fox"
>>> vowels = set('aeiou')
>>> vowels_counts = Counter(c for c in test_sentence if c in vowels)
>>> print max(vowels_counts.items(), key = lambda x: x[1])
('o', 2)

Edited by pyTony


One is greater than zero=vowels found
Three is greater than zero=vowels found

Edited by woooee


You can also go smart with this ...

using class Counter to count vowels in a text

from collections import Counter

text = '''
Python? That is for children. A Klingon Warrior
uses only machine code, keyed in on the front
panel switches in raw binary.
vowels = 'aeiou'

# method most_common() sorts by most common vowel(s) first
vowels_count = Counter(c for c in text if c in vowels).most_common()


'''result ...
[('i', 9), ('e', 9), ('o', 8), ('a', 6), ('u', 1)]
('i', 9)

You can also go all lower case with
text = text.lower()

Edited by vegaseat

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.