user525074508
user525074508

Reputation: 27

How to count vowels and consonants in Python

I'm trying to write a program that takes a user inputted sentence and outputs a count of both Vowels and Consonants in python. My Vowel counter works without any problems but the consonant count always returns 0. I have no idea why.

def checkvowelsConsonants():
    consonants = 0
    vowelscount = 0
    index = 0
    sentence = input("Please input your sentence here. This sentence must contain a speical character eg, - ? etc.")
    Vowels = (["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"])
    vowelcount = 0
    while index < len(sentence):
        if sentence[index] in Vowels:
            vowelcount += 1
        index += 1
    while index < len(sentence):
        if sentence[index] not in Vowels:
            consonants += 1
        index += 1
    print("The number of vowels is", vowelcount, "and consonants is", consonants)

here is an output I recieved for refrence

Please input your sentence here. This sentence must contain a speical character eg, - ? etc.aaaaaaaasssssssssss The number of vowels is 8 and consonants is 0

Upvotes: 1

Views: 4086

Answers (3)

Ironkey
Ironkey

Reputation: 2607

def classify_letter(string: str):
    vowels = 0
    consonants = 0
    for i in string:
        if i.casefold() in 'aeiou':
            vowels += 1
        elif i.casefold() in 'qwrtypsdfghjklzxcvbnm':
            consonants += 1
    return vowels, consonants

or with a list comprehension

def classify_letter(strng: str) -> tuple:
    x = [i.casefold() in "aeiou" for i in strng if i.casefold() in string.ascii_lowercase]
    return x.count(True), x.count(False)

^ I acknowledge this might not be the best way to implement that, im open to suggestions to make it shorter and optimized!

when passed "hello-" these both return:

(2,3)

Upvotes: 2

AMC
AMC

Reputation: 2702

The issue, as was pointed out here, is that the index variable is not reset after counting the vowels, and so your program doesn't iterate over the string again to count consonants.

In fact, the index variable should be done away with entirely. There are a few other issues with the code, too: The most important one is the fact that you're counting anything which isn't a vowel as a consonant. The string ',' isn't a vowel, but it certainly isn't a consonant.

Here is a refactored version of your program:

def count_vowels_consonants(str_in: str):
    vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
    consonants = {'w', 'm', 'j', 'r', 'z', 'c', 'h', 'l', 'p', 'k', 's', 'b', 'y', 'n', 'f', 'q', 't', 'd', 'x', 'g',
                  'v', 'L', 'S', 'N', 'Q', 'F', 'B', 'R', 'Z', 'X', 'J', 'D', 'H', 'V', 'C', 'P', 'G', 'K', 'T', 'Y',
                  'M', 'W'}
    num_vowels = 0
    num_consonants = 0

    for char in str_in:
        if char in vowels:
            num_vowels += 1
        elif char in consonants:
            num_consonants += 1

    return num_vowels, num_consonants


sentence = input('Please input your sentence (this sentence must contain a speical character eg, - ? etc.): ')

sentence_num_vowels, sentence_num_consonants = count_vowels_consonants(sentence)

print('vowels: {}, consonants: {}'.format(sentence_num_vowels, sentence_num_consonants))

I also improved the names, and moved code around to ensure the function is pure and easy to reuse.

Upvotes: 1

smcbot
smcbot

Reputation: 25

The problem here is that you don't reset your index variable after counting vowels. Therefore, when it gets to the consonant loop, the index variable is already set to whatever the sentence length is, and the consonant loop is skipped. Something like this would fix it:

def checkvowelsConsonants():
    consonants = 0
    vowelscount = 0
    index = 0
    sentence = input("Please input your sentence here. This sentence must contain a speical character eg, - ? etc.")
    Vowels = (["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"])
    vowelcount = 0
    while index < len(sentence):
        if sentence[index] in Vowels:
            vowelcount += 1
        index += 1
    index = 0
    while index < len(sentence):
        if sentence[index] not in Vowels:
            consonants += 1
        index += 1
    print("The number of vowels is", vowelcount, "and consonants is", consonants)

Notice how after the first loop, there is the index = 0 line. This resets the variable so that it is ready to go through the loop again.

Upvotes: 1

Related Questions