Home > front end >  count total vowels or consonants in a string Python
count total vowels or consonants in a string Python

Time:10-18

Question: Write a function letter_count() that will take in a list or tuple of strings and a string state and return the number of letters that satisfy the string state.

      def lettercount(strings, state) :
        if type(strings) != list and type(strings) != tuple :
            raise TypeError("First input is not a list or a tuple")
        if type(state) != str :
            raise TypeError("String state strmust be a string!")
        if state != "vowels" and state != "consonants" :
            raise ValueError("String state str may only be ‘vowels’ or ‘consonants’.")
        vowels = "aeiou"
        letters = ""
        if len(strings) == 0 :
            return 0
        i = 0
        while i < len(strings) :
            if type(strings[i]) != str :
                raise InterruptedError("{} is not a string!".format(strings[i]))
            letters  = strings[i]
            i  = 1
        counter = 0
        i = 0
        while i < len(letters) :
            j = 0
            while j < len(vowels) :
                if type(state) == "vowels" :
                    if letters[i].lower() == vowels[j] :
                        counter  = 1
                if type(state) == "consonants" :
                    counter= i 1
                j  = 1
            i  = 1
        return counter
    print(letter_count(['hello','my','name','is','V@#$'], 'vowels')) #5
    print(letter_count(['asdf'], 'consonants')) #3
    print(letter_count([], 'vowels')) #0
    print(letter_count(['qwe#$%'], 'consonants')) #2

I don't understand why consonants can't be calculated. Help me, please

CodePudding user response:

I found this code. I think it may help you!

str=input("Please enter a string as you wish: ")
vowels=0
consonants=0

for i in str:
    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("The number of vowels:",vowels)
print("\nThe number of consonant:",consonants)

Source: https://code4coding.com/python-program-to-count-vowels-or-consonants-of-the-given-string/

CodePudding user response:

checking like this will always return false

if type(state) == "vowels" :

and

if type(state) == "consonants" :

will return str, you just need to check like this

if state == "vowels" :

and

if state == "consonants" :

I have modified your code a bit to return the result you expected

def letter_count(strings, state):
    if type(strings) != list and type(strings) != tuple :
        raise TypeError("First input is not a list or a tuple")
    if type(state) != str :
        raise TypeError("String state strmust be a string!")
    if state != "vowels" and state != "consonants" :
        raise ValueError("String state str may only be vowels or consonants.")
    vowels = "aeiou"
    letters = ""
    if len(strings) == 0 :
        return 0
    i = 0
    while i < len(strings) :
        if type(strings[i]) != str :
            raise InterruptedError("{} is not a string!".format(strings[i]))
        letters  = strings[i]
        i  = 1
    counter = 0
    i = 0
    while i < len(letters) :
        if state == "vowels" :
            if letters[i].lower() in vowels:
                counter  = 1
        if state == "consonants" :
            if letters[i].lower() not in vowels and letters[i].isalnum():
                counter  = 1
        i  = 1
    return counter
print(letter_count(['hello','my','name','is','V@#$'], 'vowels')) #5
print(letter_count(['asdf'], 'consonants')) #3
print(letter_count([], 'vowels')) #0
print(letter_count(['qwe#$%'], 'consonants')) #2

output

5
3
0
2

CodePudding user response:

        j = 0
        while j < len(vowels) :
            if type(state) == "vowels" :
                if letters[i].lower() == vowels[j] :

Should be

        if letters[i].lower() in vowels:
  • Related