Vowel counter | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Vowel counter

str = input() counter = 0 for s in str: if s in 'aeiouAEIOU': counter += 1 print (counter) In above problem why it can't be that if s == 'a' or 'e' or 'i' or 'o' or 'u'

2nd Aug 2021, 6:05 PM
Ravi
9 Answers
+ 10
a short version but still with a good readability: (it uses a list comprehension. each time the conditional expression evaluates to True, the value 1 is send to a "virtual" list. finally the sum of all the numbers will be calculated by using sum() function and printed out) text = input().lower() print(sum([1 for i in text if i in "aeiou"]))
2nd Aug 2021, 8:23 PM
Lothar
Lothar - avatar
+ 6
Calvin Thomas , nice code, that collects all the True and False 'values' that are created in the generator object. and it uses a side effect that all True values are treated as value 1 and all False as value 0, so that sum() function can be used. 👍👍 on the other hand: this is tricky, but not very obvious and so i have my doubts in using it in real world coding or apps 🤔
3rd Aug 2021, 10:50 AM
Lothar
Lothar - avatar
+ 5
It can be but in your code, But you must compare every time with s .It should be: if s == 'a' or s =='e' or s =='i' or s =='o' or s =='u' But it will work only for lowercase. So you can do a little trick. You can make all the str letters lowercase. Here's the code: https://code.sololearn.com/ca22a07A9a15
2nd Aug 2021, 6:14 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 2
It can be that, it's just that "in" is shorthand code for saying "is this letter in this sequence". If s is a vowel, then the result of "s in 'aeiouAEIOU' " will be true. You can write it as a boolean expression, but it's just easier to use this built in functionality. And, of course, the string method also includes the upper case versions too, not just lower case.
2nd Aug 2021, 6:10 PM
BootInk
BootInk - avatar
+ 2
Ravi How about this one? :- print(sum(x in "aeiou" for x in input().lower())) # Hope this helps
3rd Aug 2021, 6:23 AM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Almost each answer has the sentence about that the second variant is incorrect or not recommended, but no one has yet explained why it is so. If you do not care about code size and productivity then you can use variant that is similar to the second one, but what you mentioned does different thing. `==` operator's precedence is higher than `or`'s, so that `something == something_else` is evaluated before `something or something_else` and so it is evaluated as that: 1. s == 'a' If 1. == True then it is obvious that `True or something` is True, so execute "if" block; Otherwise: 2. 'e' is non-blank sequence and so is evaluated as True; execute "if" block. Even if those operators' precedence was different, it would still not work properly: `or` operator returns the first operand if it is True and the second operand otherwise; so `'a' or something` is 'a' for any of those `or`'s and is compared with s and so it would be equivalent to simple `s == 'a'`. I hope that you now understand all those answers.
4th Aug 2021, 5:46 PM
#0009e7 [get]
#0009e7 [get] - avatar
+ 1
Hi Ravi! Your previous method is easier. But, it can be done by comparing each character with s. But your code needs to be like this, not the way you mentioned above if s=='a' or s=='e' or s=='i' so and so
2nd Aug 2021, 6:15 PM
Python Learner
Python Learner - avatar
0
Lothar Well, I came across David Ashton's answer and thought of modifying a bit, including the conversion of the input into its lower form.
3rd Aug 2021, 12:09 PM
Calvin Thomas
Calvin Thomas - avatar