Aaditya Thakkar
Aaditya Thakkar

Reputation: 25

Count vowels of each word in a string

Refer to this image1

Hey, this is a program i want to write in python. I tried, and i sucessfully iterated words but now how do i count the individual score?

a_string = input("Enter a sentance: ").lower()
vowel_counts = {}
splits = a_string.split()
for i in splits:
    words = []
    words.append(i)
    print(words) 

Upvotes: 1

Views: 127

Answers (1)

Alain T.
Alain T.

Reputation: 42133

You can flag the vowels using translate to convert all the vowels to 'a's . Then count the 'a's in each word using the count method:

sentence = "computer programmers rock"

vowels  = str.maketrans("aeiouAEIOU","aaaaaaaaaa")
flagged = sentence.translate(vowels)                    # all vowels --> 'a'
counts  = [word.count('a') for word in flagged.split()] # counts per word
score   = sum(1 if c<=2 else 2 for c in counts)         # sum of points

print(counts,score)    
# [3, 3, 1] 5

Upvotes: 1

Related Questions