+ 3

Is there Any other Simple Solution??

Write a program that takes in a string as input, counts and outputs the number of vowels (A, E, I, O, U). Input Format: A string (letters can be both uppercase or lower case). Output Format: A number which represents the total number of vowels in the string.

18th Dec 2024, 1:51 PM
HASAN IMTIAZ
HASAN IMTIAZ - avatar
9 Answers
+ 7
HASAN IMTIAZ , your code is absolutely ok, just modify it as Lisa mentioned. (don't be too much `impressed` by the codes from RuntimeTerror and from Brian ) -> readability counts! the code snippet below has a different approach compared to yours, and is iterating over the vowels string instead over the input text. to get the number of each vowel we use the count() method of the string class. user_input = input().lower() vowels = "aeiou" vowel_count = 0 for char in vowels: vowel_count += user_input.count(char) print(vowel_count)
18th Dec 2024, 8:48 PM
Lothar
Lothar - avatar
+ 5
you can simplify your code by defining vowels as vowel = "aeiou" (it does not need to be a list of individual characters) and you do not need to sort the user_input.
18th Dec 2024, 2:23 PM
Lisa
Lisa - avatar
+ 4
HASAN IMTIAZ for strings, the 'in' operator checks if the value is a substring. 'Alya' in 'Rayla' would return False 'ayla' in 'Rayla' would return True in Brian's code, char in 'aeiou' returns True everytime char is a vowel. True in Python is also treated as 1. The sum function adds up the 1's to give the vowel count
19th Dec 2024, 9:50 AM
Bob_Li
Bob_Li - avatar
+ 3
Lothar Sure, I'd go with yo answer too but I found an opportunity to use << lol đŸ€ŁđŸ€Ł I'm not letting it slide
18th Dec 2024, 10:08 PM
RuntimeTerror
RuntimeTerror - avatar
+ 2
user_input = input().lower() vowel =["a","e","i","o","u"] text = sorted(user_input) vowel_count = 0 for i in text: if i in vowel: vowel_count += 1 print(vowel_count)
18th Dec 2024, 1:51 PM
HASAN IMTIAZ
HASAN IMTIAZ - avatar
+ 2
#This was my simplest solution: print(sum(char in 'aeiou' for char in input().lower()))
18th Dec 2024, 2:38 PM
Brian
Brian - avatar
+ 2
From Brian, I added more condition for statement that aren't fully alphabet print(sum((1 << (ord(char.lower()) - 0x61)) & 0b10000100000100000100001 > 0 for char in input() if char.isalpha()))
18th Dec 2024, 6:14 PM
RuntimeTerror
RuntimeTerror - avatar
+ 1
So......How Does *in* Keyword works When we Check a String. For instance, if "Alya" in "Rayla": How does The Code will Work..? Please Explain me
19th Dec 2024, 3:41 AM
HASAN IMTIAZ
HASAN IMTIAZ - avatar
0
Brian ,Your Codes Are Working But I have No idea how
18th Dec 2024, 5:04 PM
HASAN IMTIAZ
HASAN IMTIAZ - avatar