Create a program that receives a line of text and counts the vowels with their respective histogram

0

exercise: Create a program that receives a line of text and counts the vowels with the respective histogram as follows:

  

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

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

I did this:

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, I would have to do the vowel by vowel. I would like to know in a more practical way to do because for example if the exercise asked for letters of the alphabet everything would be too big to do everything.

    
asked by anonymous 07.10.2018 / 20:18

1 answer

3

Just you 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, ')')

Do not forget that your logic will only work for lowercase letters without an accent.

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

from string import ascii_letters
    
07.10.2018 / 20:30