Count the number of vowels and consonants present in a string. (Input must be given by user and blank space shouldn’t be counted | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Count the number of vowels and consonants present in a string. (Input must be given by user and blank space shouldn’t be counted

Guys, Please help me to solve the Error in code. PROGRAM CODE: def countCharacterType(str): vowels = 0 consonant = 0 for i in range(0, len(str)): s=str(i) if ((s >= 'a' and s <= 'z') or (s >= 'A' and s <= 'Z')): s = s.islower() if (s == 'a' or s == 'e' or s == 'i' or s == 'o' or s == 'u' or s == 'A' or s == 'E' or s == 'I' or s == 'O' or s == 'U'): vowels += 1 else: consonant += 1 print("Vowels:", vowels) print("Consonant:", consonant) str = input("Enter a string from user: ") countCharacterType(str)

9th Aug 2020, 6:51 AM
MA VANSHIT PATEL
MA VANSHIT PATEL - avatar
3 Answers
+ 12
You really only need 4 lines: string = "".join(input().split()) # removes spaces vowels = [c for c in string if c in "aeiouAEIOU"] print("vowels:", len(vowels)) print("consonants:", len(string) - len(vowels))
10th Aug 2020, 9:25 AM
David Ashton
David Ashton - avatar
+ 2
Hi MA VANSHIT PATEL, There are a few errors in your code: 1. You are using a very weird indentation. There are whitespace and tabs mixed.. please only use spaces OR tabs for indentation. please find out, which you prefer more, and do it this way consequently.. 2. You assign s.islower() to s. s.islower is a function to check, is s consists of lower case characters only... you should rather use s.lower(), which converts upper case to lower case... 3. single characters on a certain index are accessed via square brackets, not round brackets... And then there are some hints: 1. Avoid using names of python built-ins, like str.. 2. Use pythonesk way to iterate over iterables: for i in range(0,len(str)) --> for s in str 3. Avoid very long if condition... Use a list instead: vowels_list = ['a', 'e', 'i', 'o', 'u', ...] if s in vowels_list: ...
9th Aug 2020, 7:21 AM
G B
G B - avatar
+ 1
Your code with corrections: vowels_lst = ['a','e','i','o','u'] def countCharacterType(wrd): vowels = 0 consonant = 0 for s in wrd: s= s.lower() if s in vowels_lst: vowels += 1 else: consonant += 1 print("Vowels:", vowels) print("Consonant:", consonant) str_in = input("Enter a string from user: ") countCharacterType(str_in)
9th Aug 2020, 7:22 AM
G B
G B - avatar