Create a program that receives a line of text and counts the vowels showing the respective Histogram

Asked

Viewed 172 times

0

exercico: Prepare a program that receives a line of text and counts the vowels showing the respective Histogram a in the following form:

Example: Last line of text: "Next fourth line of text is holiday."

 a :  ****** (6) 
 e :  *** (3) 
 i :  *** (3)  
 o :  ** (2) 
 u :  * (1)  

I did it this way:

texto = 'na proxima quarta-feira é feriado'
a = texto.count('a')

print('a:','*'*a,'(',a,')')

the way I did does exactly what the exercise asks for but I would have to make vowel by vowel. I would like to know a more practical way of doing it because for example if the exercise asked for letters of the whole alphabet it would be too big to do everything.

1 answer

3


Just iterate through all the vowels and put the logic inside the loop.

texto = 'na proxima quarta-feira é feriado'
for vogal in 'aeiou':
    n = texto.count(vogal)
    print('a:', '*' * n, '(', n, ')')

Don’t forget that your logic will only work for lowercase letters and no accent.

If you wanted to do all the letters without having to write the whole alphabet, Python offers a ready list of characters:

from string import ascii_letters

Browser other questions tagged

You are not signed in. Login or sign up in order to post.