+ 1

Need help to understand detection in a string and other operations in python.

Hello, I was trying to make a code that detects how many vowels, consonants, spaces, and symbols are there in a given string but there's something I'm doing wrong and I can't tell that. Would you please be kind enough to explain what causes the error and what I should be doing? Thank you very much 💙 https://code.sololearn.com/c5AZUyrQpiv2/?ref=app

3rd Jan 2020, 9:06 AM
Bishal Poonia (Bishu)
Bishal Poonia (Bishu) - avatar
5 ответов
+ 5
You are really very close, only a few changes are needed to get the result. I am sure you can understand the code. If there are some doubts, just come back to ask. (at the moment digits are counted as symbols. But you can change that) myString = 'This is a string!'.casefold() alphabets = 'abcdefghijklmnopqrstuvwxyz' vow_ = 'aeiou' vowels = [] consonants = [] spaces = [] symbols = [] for i in myString: #if i in alphabets or alphabets.upper if i in vow_: vowels.append(i) elif i in alphabets and i not in vow_: consonants.append(i) elif i == ' ': spaces.append(i) else: symbols.append(i) print("Vowels: ", len(vowels), "\n→", vowels) print("Consonants: ", len(consonants), "\n→", consonants) print("Spaces: ", len(spaces), "\n→", spaces) print("Symbols: ", len(symbols), "\n→", symbols)
3rd Jan 2020, 9:51 AM
Lothar
Lothar - avatar
+ 3
HonFu That was Exactly the only thing that made entire code from messy to perfect! Thanks a lot. Also, the example you used is iconic xD never gonna forget that for sure!
3rd Jan 2020, 10:28 AM
Bishal Poonia (Bishu)
Bishal Poonia (Bishu) - avatar
+ 2
if i in alphabets or alphabets.upper() This line does not do what you probably think. It is read as: if (i in alphabets) or (alphabets.upper()) You probably mean: if i in alphabets or i in alphabets.upper() So alphabets.upper() is treated as a separate statement. What will happen? alphabets will be uppered, and then it will be checked for truthyness. So is an uppered string True? Yes, every single time, as long as it contains anything. So basically, you never leave this if branch! It's a common mistake, because people say stuff like: Please bring me a cup of coffee or tea. Python might understand: Please bring me a cup of coffee - or tea, but without the cup. 😁
3rd Jan 2020, 9:37 AM
HonFu
HonFu - avatar
+ 2
Lothar You're right! It totally slipped off my mind! I didn't realize the code will interpret the numbers as symbols, thank you! Really appreciate the effort you put in writing this entire block of code so I can understand it better, thanks a lot!
3rd Jan 2020, 10:26 AM
Bishal Poonia (Bishu)
Bishal Poonia (Bishu) - avatar
0
mystr = 'Hello! Hello! Mic Testing - 1,2,3' print('Vowel :',len([i for i in mystr.lower() if i in 'aeiou'])) print('Consonant :',len([i for i in mystr.lower() if i.isalpha() and i not in 'aeiou'])) print('Space :',len([i for i in mystr.lower() if i in ' '])) print('Numbers :',len([i for i in mystr.lower() if i.isdigit()])) #------another way, in single line------ print() print('Vowel :',len([i for i in mystr.lower() if i in 'aeiou']), '\nConsonant :',len([i for i in mystr.lower() if i.isalpha() and i not in 'aeiou']), '\nSpace :',len([i for i in mystr.lower() if i in ' ']), '\nNumbers :',len([i for i in mystr.lower() if i.isdigit()]) )
29th May 2020, 1:24 AM
Akbar Ali Quazi
Akbar Ali Quazi - avatar