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

Question:

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:

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")

Asked By: maria04

||

Answers:

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'])
Answered By: Michael M.

I would write the code to achieve your result as follows:

import string

# Create the alphabet characters list
lowercase_letters = list(string.ascii_lowercase)
uppercase_letters = list(string.ascii_uppercase)
letters = lowercase_letters + uppercase_letters

# Get the input sentence
string = input()

# Get the words in the input sentence
words = string.split()

# Count the number of times of 'the'
counted_the = words.count("the")

# Get the list of valid characters in the input sentence
chars = [char for char in string if char != " " and char in letters]

# Vowels list
vowels = {"a":0, "A":0, "e":0, "E":0, "i":0, "I":0, "o":0, "O":0, "u":0, "U":0}

# Calculate the number of times of vowels
for key, value in vowels.items():
    counted = chars.count(key)
    vowels[key] = counted
    if counted > 0:
        print(key, " *" * counted)

# Print the results
if counted_the == 0:
    print("zero")
else:
    print(counted_the)

The result will be like this (given the input sentence "this is a test count the vowels and how many the Is in the sentence"):

a  * * *
e  * * * * * * * *
i  * * *
I  *
o  * * *
u  *
3
Answered By: hsaltan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.