0

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

Michael M.
  • 10,486
  • 9
  • 18
  • 34
maria04
  • 21
  • 4
  • "I want to have them like this:" Okay; so what **difficulty do you encounter** when you try to fix the code to do that? For example, if you have the value `3`, do you see how to use that in order to create a string like `***` and then display it? given that the variable `acount` is equal to `3`, do you see how to produce the output `a ***`? And so on for the other vowels. – Karl Knechtel Nov 25 '22 at 10:11

2 Answers2

1

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
Michael M.
  • 10,486
  • 9
  • 18
  • 34
hsaltan
  • 461
  • 1
  • 3
  • 13
  • Hey Thanks! I could easily catch up and this is also a great solution. I'll use your help thank you :) – maria04 Nov 12 '22 at 15:33
0

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'])
Michael M.
  • 10,486
  • 9
  • 18
  • 34
  • Thank you so much! For the edit and also for the answer. It's working correctly for the vowels part but how can I have the "zero" output for "the" part if there was no "the" in the sentence? – maria04 Nov 12 '22 at 14:57
  • @hAsTia.r.m.y Oh, I see what you want to do. I've updated my answer to spell 'zero' if there are no instances of 'the' – Michael M. Nov 12 '22 at 14:59
  • 1
    Hurray! Thanks it's fully working. :) – maria04 Nov 12 '22 at 15:01