Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a histogram to display the frequency of vowels (a-e-i-o-u) from an input in Python with (*)

So I’ve currently made a progress so far that I can get how many times letters (a-e-i-o-u) have been written in the sentence which was taken as an input. Also if there’s any "the" in the sentence we should count them too. and at the end we should get something like this:
e.g:
input: Why little Dora herself came crying loud
output:

a **
e ****
i **
o **
u *
zero (mentions how many times "the" was used)

I couldn’t get to find how to put (*) as in times that letter was used in the sentence but I could just take them out as numbers.

allowed_chars = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ")
string = input()
validation = set((string))
if validation.issubset(allowed_chars):
    pass
else:
    print("error")
a = "a"
A = "A"
e = "e"
E = "E"
i = "i"
I = "I"
o = "o"
O = "O"
u = "u"
U = "U"
acount = 0
ecount = 0
icount = 0
ocount = 0
ucount = 0
for v in string:
    if(v==a or v==A):
        acount = acount + 1
    if (v==e or v==E):
        ecount = ecount + 1
    if (v==i or v==I):
        icount = icount + 1
    if (v==o or v==O):
        ocount = ocount + 1
    if (v==u or v==U):
        ucount = ucount + 1
print(acount,ecount,icount,ocount,ucount)
word = "the"
words = string.split()
thecount = 0
for w in words:
    if w == word:
        thecount += 1
print(thecount)

sample input for this code:
this is a test count the vowels and how many the Is in the sentence
output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

3 8 4 3 1 
3

I want to have them like this:

a ***
e ********
i ****
o ***
u *
3

(and if there was no "the" just print "zero")

>Solution :

Your code can be simplified with a collections.Counter:

import collections

allowed_chars = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ")
text = input().lower()

if not set(text).issubset(allowed_chars):
    print("Invalid input")
    exit(1)

count = collections.Counter(text)
count['the'] = text.split(' ').count('the')

for letter in ['a', 'e', 'i', 'o', 'u']:
    print(f'{letter} {"*" * count[letter]}')

if count['the'] == 0:
    print('zero')
else:
    print(count['the'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading