Print total number of vowel and consonant in an entered sentence After asking my first question got a nice review on that. So, this is my solution for a different question checking edge cases and incorporating ideas given in that review.


  Question: Write a program to count total number of vowel and consonant characters present in an user entered sentence?
  
  Expected Output: If user inputs,
  
  
  1CHELSEA. 1FOOTBALL. 1CLUB. -> Vowel: 7, Consonant: 12
  a1?3e5i6!@o7^&amp;*8)u -> Vowel: 5, Consonant: 0
  q1?32536!@47^&amp;*8)p -> Vowel: 0, Consonant: 2
  


My solution works for all expected cases and from previous review suggestion I tried docstring, doctest, etc. So from this review I would like to know whether I am using those correctly or not, and if there's easier and better way to do this.

My solution:

from sys import exit


def counter(sentence):
    """Removes whitespaces, punctuation and capitalize every words.

    &gt;&gt;&gt; counter("1CHELSEA. 1FOOTBALL. 1CLUB.")
    Vowel: 7, Consonant: 12

    &gt;&gt;&gt; counter("a1?3e5i6!@o7^&amp;*8)u")
    Vowel: 5, Consonant: 0

    &gt;&gt;&gt; counter("q1?32536!@47^&amp;*8)p")
    Vowel: 0, Consonant: 2

    """
    count_sentence = "".join(ch for ch in sentence if ch.isalpha()).upper()  # removes punctuation and numbers

    def find_total(count):
        """Check if character is present in vowel_list if present ++ in total"""
        vowel_list = ["A", "E", "I", "O", "U"]
        total = 0
        for character in count:
            if character in vowel_list:
                total += 1
        return total

    find_total(count_sentence)
    vowel_total = find_total(count_sentence)
    consonant_total = len(count_sentence) - vowel_total
    print("Vowel: {0}, Consonant: {1}".format(vowel_total, consonant_total))
    return


def main():
    """Checks for blank input."""
    entered_sentence = input("Please enter a sentence?\n&gt;")
    if entered_sentence.strip() == "":
        exit("Program Terminated. (No input)")
    else:
        counter(entered_sentence)
    return


if __name__ == '__main__':
    import doctest
    doctest.testmod(main())


Lastly, please help me on returning from a function and separating printing part not been able to understand that.
Some thoughts:

choose good names

counter is IMO a bit "generic". Perhaps something more expressive like count_vowels_consonants?

Moreover, the inner function's parameter is named count, but it's actually not some count (number) but a text string.

Furthermore you're using the term "sentence", but you're not actually requiring sentences, but rather character sequences / strings.

don't call the function twice

find_total(count_sentence)
vowel_total = find_total(count_sentence)


Here you call find_total, discard its result (return value) and then call it again, this time assigning the result to the local variable.

separate calculation and output

Instead of

print("Vowel: {0}, Consonant: {1}".format(vowel_total, consonant_total))
return


leave out the print and return the values in a tuple

return (vowel_total, consonant_total)


On the caller side, you can then either use the returned tuple as it is

t = count ("foo bar")


or for example use tuple unpacking to obtain the individual counts

vowels, consonants = count ("foo bar")


only iterate once over the characters

Instead of first going one time over the characters to filter out non alphabetic characters and adjust the case of the alphabetic ones and then a second time to count, do it in one loop:

def count_vowels_consonants(text):
  vowels_list = ['A', 'E', 'I', 'O', 'U']
  consonants = 0
  vowels = 0
  for character in text:
    if character.isalpha():
      if character.upper() in vowels_list:
        vowels += 1
      else:
        consonants += 1
  return (vowels, consonants)


test corner cases

Does your function work well with an empty string? A string with only vowels or consonants? A string with only punctuation?