How To Count Vowels And Consonants In Python?
Solution 1:
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 = 0whileindex < len(user_input):
if user_input[index] in vowel_list:
vowels += 1index += 1print ('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:
forchar in user_input:
ifchar in vowel_list:
vowels += 1
Solution 2:
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([charforcharin user_input ifcharin 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([charforcharin user_input ifcharin vowel_list])
print ('Your input has', vowels , 'vowels.')
def countConsonants(user_input, vowel_list):
vowels = len([charforcharin user_input ifcharin vowel_list])
print ('Your input has', len(user_input)-vowels, 'consonants.')
main()
Solution 3:
##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 vowelsfor i in listChar : ## use loop for count eaxh elements
c += 1
if i in'aeiou' : ## check it is vowewl
cVowel = cVowel + 1 # count vowelelif i in'!@#$%^&*()+-*/123456789~`' : # other than alphabets
cOther = cOther + 1 # count other than alphabets elementselse :
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)
Solution 4:
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.
defcountVowels(user_input, vowel_list):
vowels = 0for i in vowel_list:
if i in user_input:
vowels+=1print ('Your input has {} vowels.'.format(vowels))
defcountConsonants(user_input, count_Consonants):
consonants = 0for i in count_Consonants:
if i in user_input:
consonants+=1print ('Your input has {} consonants.'.format(consonants))
defmain():
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()
Solution 5:
def VowCons(Str):
Vcount =0
ConsCount = 0
Vowels = ['a','e','i','o','u']
forchar in Str:
ifchar in Vowels:
Vcount +=1else:
ConsCount +=1print(Vcount," is the number of vowels and ",ConsCount," is the count of consonants")
VowCons("how many vowels and consonants are there")
Post a Comment for "How To Count Vowels And Consonants In Python?"