Python Problem: Get count of Vowel and Consonant letters
Problem Statement
the problem states to write functions that accepts string as argument, and return vowels and consonants. the program gets user’s string input and display number of vowels and consonants based on the functions
input:
user’s string input
output:
display of how many consonants and vowels in the string
Solution Design
- assign vowels list
- build function to count vowel
- build function to count consonant
- build function to get and process the user’s string
- call the functions
- display the counter for both vowels and consonants
assumption:
user enter a non blank string
Code
'''
Author: Raditya Fahritama
---Variables in code---
vowels = list of vowels
vowelcount = count of vowels in string
conscount = count of consonant in string
loop = flag for loop
str = user's input string
'''
import string
#assign vowels list
vowels = ['a', 'e', 'i', 'o', 'u']
#build vowel counter function
def vowelcounter(st):
vowelcount = 0
for x in st:
#check if the character is in vowels list
if x in vowels:
vowelcount += 1
return vowelcount
#-----------------------
#build consonant counter function
def consonantcounter(st):
conscount = 0
for x in st:
#check if the character is not in vowels list
if x not in vowels:
conscount += 1
return conscount
#-----------------------
#build get-string and process-string function
def getprocessstr():
loop = 1
while loop == 1:
#get input, lower input and strip whitespaces
str = input("\nEnter a string: ").lower().replace(" ", "")
if str == "":
#exception handling
print("\nPlease enter a string!")
else:
loop = 0
#remove symbols and punctuations
for char in string.punctuation:
str = str.replace(char,'')
#join string and remove numbers
str = "".join(filter(lambda x: x.isalpha(), str))
return str
#-----------------------
#call getprocessstring function
str = getprocessstr()
#call vowel counter function
vowelcount = vowelcounter(str)
#call consonant counter function
conscount = consonantcounter(str)
#display the results
print("\nVowel count : ",vowelcount,"\nConsonant count : ",conscount)
Testing & Exception Handling
testing:
- inputting blank string
- inputting string (normal case)
exception handling:
- for blank string, the program will simply tell the user to enter a string
- all exception is in loop. so that the user doesn’t have to re-run the program when they raises exception. the loop breaks when everything is in normal case
Discussion
the program seems to run the intended solution well based on the output that it provided. I decided not to classify spaces and characters other than alphabet as invalid characters. i decided to lower string to prevent ambiguity. i also strip white spaces, remove punctuations and number. for example, if the string is like “asd asd123 asd@”, after the process, the string will be “asdasdasd”. i think it will be easier to count it like that. the program also handle stated exception well. the program solved the problem statement of the question number 9.