Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
"""a program that asks the user
for the number of lines for the entered poem.
It then allows the user to enter the desired number of lines.
Then output the number of lines,
vowels and consonants in the poem and in each line."""

I tried this for a string.
But I am not able to think of vowels and consonants in each line.

What I have tried:

Python
strg = input("Please Enter Your Own String : ")
vowels = 0
consonants = 0

for i in strg:
     if (i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'
            or i == 'A' or i == 'E' or i == 'I' or i == 'O' or i == 'U'):
        vowels = vowels + 1
     else:
        consonants = consonants + 1

print("Number of Vowels: ", vowels)
print("Number of Consonants: ", consonants)
Posted
Updated 23-Sep-22 17:18pm
v3
Comments
Richard MacCutchan 25-May-21 3:32am    
Your if statement could be simplified to:
if i in 'aeiouAEIOU':
    vowels = vowels + 1

Put that code in a function.
Call it for each line.

You'll have to make a few changes but anyone who can write that code shouldn't have any problem doing that as it's pretty easy stuff.
 
Share this answer
 
Quote:
a program that asks the user
for the number of lines for the entered poem.

This means that user input is sentences,which imply that input contains non letters like spaces, commas or points.
Python
if (i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'
       or i == 'A' or i == 'E' or i == 'I' or i == 'O' or i == 'U'):
   vowels = vowels + 1
else:
   consonants = consonants + 1

So, what is not a vowel is not necessary a consonant, you need to check if a char is a consonant, or not.
 
Share this answer
 
strg = input("Please Enter Your Own String : ")
vowels = 0
consonants = 0

for i in strg:
if i in 'aiueoAIUEO':
vowels = vowels + 1
elif i in 'bBcBdDfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXyYzZ':
consonants = consonants + 1

print("Number of Vowels: ", vowels)
print("Number of Consonants: ", consonants)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900