Part_Time_Nerd
Part_Time_Nerd

Reputation: 1014

How to count vowels and consonants in Python?

I am trying to write a program that counts vowels and consonants in Python, then print the number of vowels and consonants in two statements. The vowels and consonants have to be in two different functions. I have most of it complete but I cannot figure out two errors.

1.) How to stop my script from printing a new line for each count of the vowels. I have tried numerous variations of accumulators and print statements but none seem to work.

2.) I cannot get my countConsonants function to run at all. I am assuming I would have similar problems to number one but I cannot get it to even run. I am assuming it has to do with the way I am calling the function from the main function but I am not sure.

Here is what I have:

def main():
   user_input = input("Enter a string of vowels and consonants: ")
   vowel_list = set(['a','e','i','o','u'])
   countVowels(user_input, vowel_list)
   countConsonants(user_input, vowel_list)

def countVowels(user_input, vowel_list):
    user_input.lower()
    index = 0
    vowels = 0

    while index < len(user_input):
        if user_input[index] in vowel_list:
            vowels += 1
            index += 1
            print ('Your input has', vowels , 'vowels.')

def countConsonants(user_input, vowel_list):
    user_input.lower()
    index = 0
    consonants = 0

    while index < len(user_input):
        if user_input[index] not in vowel_list:
            consonants += 1
            index += 1
            print ('Your input has' , consonants , 'consonants.')

main()

Here is an IO:

Enter a string of vowels and consonants: aaeioulkjj
Your input has 1 vowels.
Your input has 2 vowels.
Your input has 3 vowels.
Your input has 4 vowels.
Your input has 5 vowels.
Your input has 6 vowels.

Upvotes: 2

Views: 11578

Answers (7)

VISHAKHA AGARWAL
VISHAKHA AGARWAL

Reputation: 11

##it is list
listChar = ['$','a','8','!','9','i','a','y','u','g','q','l','f','b','t']
c = 0##for count total no of elements in a list
cVowel = 0 # for count vowel
cConst = 0 # for count consonants
cOther = 0 # for count other than consonants and vowels
for i in listChar : ## use loop for count eaxh elements
    c += 1
    if i in 'aeiou' : ## check it is vowewl
        cVowel = cVowel + 1 # count vowel
    elif i in '!@#$%^&*()+-*/123456789~`' : # other than alphabets
        cOther = cOther + 1 # count other than alphabets elements
    else :
        cConst = cConst + 1 ## count consonants if above contion not satisfied

print all results

print ("total number of element in the list  : ", c)
print("count vowels characters : ",cVowel)
print("count consonants characters : ",cConst)
print("count other characters : ",cOther)

Upvotes: 2

Chris Charley
Chris Charley

Reputation: 6613

Here is a program that counts vowels and consonants.

It makes use of a dictionary slice:

itemgetter(*vowel_or_consonant)(c) # slice

Script:

from collections import Counter
from operator import itemgetter
from string import ascii_letters

VOW = set('aeiouAEIOU')
CON = set(ascii_letters)-VOW

def letter_count(s, vowel_or_consonant):
    c = Counter(s)
    return sum(itemgetter(*vowel_or_consonant)(c))

s ="What a wonderful day to program"

print('Vowel count =', letter_count(s, VOW))
print('Consonant count =', letter_count(s, CON))

"""
================= RESTART: C:/Old_Data/python/vowel_counter.py =================
Vowel count = 9
Consonant count = 17
"""

Upvotes: 0

Muhammad Usman
Muhammad Usman

Reputation: 1

Define a procedure is_vowel.

It takes your name as its input and prints:

 ‘Your name starts with avowel’

if it starts with a vowel, and prints ‘Your name starts with a consonant’, otherwise? Example:

is_vowel(‘Ali’) ‘Your name starts with a vowel’
is_vowel(‘aqsa’)  ‘Your name starts with a vowel’
is_vowel(‘Sam’) ‘Your name starts with a consonant’

Upvotes: 0

Ashfaq Khan
Ashfaq Khan

Reputation: 1

def VowCons(Str):
    Vcount = 0
    ConsCount = 0
    Vowels = ['a','e','i','o','u']
    for char in Str:
        if char in Vowels:
            Vcount +=1
        else:
            ConsCount +=1
    print(Vcount," is the number of vowels and ",ConsCount," is the count of consonants")

VowCons("how many vowels and consonants are there")

Upvotes: 0

Mr.Pacman
Mr.Pacman

Reputation: 360

I hope this is what you want. I replaced the while loop with the for loop and added a variable count_Consonant with a list of consonants.

def countVowels(user_input, vowel_list):
    vowels = 0
    for i in vowel_list:
        if i in user_input:
            vowels+=1
            print ('Your input has {} vowels.'.format(vowels))

def countConsonants(user_input, count_Consonants):
    consonants = 0
    for i in count_Consonants:
        if i in user_input:
            consonants+=1
            print ('Your input has {} consonants.'.format(consonants))

def main():
   user_input = input("Enter a string of vowels and consonants:  ").lower()
   vowel_list = set(['a','e','i','o','u'])
   countVowels(user_input, vowel_list)
   count_Consonants = set(["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"]) 
   countConsonants(user_input, count_Consonants)

main()

Upvotes: 1

Neil
Neil

Reputation: 14313

I read through your code and found a couple issues. You seem to not be calling .lower in the right spot. It doesn't modify the string, it simply returns a lowercase version of the string. And i combined your vowels and consonants with a little math. Additionally, I added a conditional for loop that will scan through the letters and pick all the vowels out, then it will take the length of the vowel list found. Finally, I also just combined vowel_list into a string to make it look prettier.

  def main():
     user_input = input("Enter a string of vowels and consonants: ").lower()
     vowel_list = 'aeiou'
     countVowelsAndConsoants(user_input, vowel_list)

  def countVowelsAndConsoants(user_input, vowel_list):
      vowels = len([char for char in user_input if char in vowel_list])
      print ('Your input has', vowels , 'vowels.')
      print ('Your input has', len(user_input)-vowels, 'consonants.')

  main()

If you need them both separate:

  def main():
     user_input = input("Enter a string of vowels and consonants: ").lower()
     vowel_list = 'aeiou'
     countVowels(user_input, vowel_list)
     countConsonants(user_input, vowel_list)

  def countVowels(user_input, vowel_list):
     vowels = len([char for char in user_input if char in vowel_list])
     print ('Your input has', vowels , 'vowels.')

  def countConsonants(user_input, vowel_list):
     vowels = len([char for char in user_input if char in vowel_list])
     print ('Your input has', len(user_input)-vowels, 'consonants.')

  main()

Upvotes: 2

ramcdougal
ramcdougal

Reputation: 2367

Indentation is key.

The print should only happen after the while loop is done, so it must be indented the same as the while. The increment of the index is in the wrong spot as well: this must happen each time regardless of whether or not the if condition evaluates to True. (With your alignment, the index only increases past vowels and may never get far enough to allow the while loop to end; this is why you never got to countConsonants.)

Your countVowels function then becomes:

def countVowels(user_input, vowel_list):
    index = 0
    vowels = 0

    while index < len(user_input):
        if user_input[index] in vowel_list:
            vowels += 1
        index += 1
    print ('Your input has', vowels , 'vowels.')

By the way, consider using a for loop here over the characters in user_input instead of while and indexing; i.e. use something like:

for char in user_input:
    if char in vowel_list:
        vowels += 1

Upvotes: 2

Related Questions