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'
9 Respostas
+ 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"]))
+ 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 🤔
+ 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
+ 5
Here's a short one
https://code.sololearn.com/c3tXHv6wjGAg
and a few others
https://code.sololearn.com/c97WkmUyDrNF
https://code.sololearn.com/c0Z00UHNmoH0
https://code.sololearn.com/cDUPF29E3d84
+ 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.
+ 2
Ravi How about this one? :-
print(sum(x in "aeiou" for x in input().lower()))
# Hope this helps
+ 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.
+ 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
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.