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)

Recommended Answers

All 5 Replies

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()
vowel_found=False

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

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)

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

Thanks you guys, helped alot :)

You can also go smart with this ...

'''count_vowels1.py
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()

print(vowels_count)
print(vowels_count[0])

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.