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

Vowel counter

Hey guys, I was thinking of how can I improve this python code by using a loop or at least making it shorter. The code works fine for the problem (You need to make a program that counts the number of vowels in a given text. Take a string as input and output the number of vowels.) but I think it's very basic. x = list(input()) cont_a = x.count("a") cont_e = x.count("e") cont_i = x.count("i") cont_o = x.count("o") cont_u = x.count("u") total = cont_a + cont_e + cont_i + cont_o + cont_u print(total) Thank you!

3rd Mar 2021, 8:59 PM
Santiago Torres Busquets
Santiago Torres Busquets - avatar
22 Answers
+ 30
1️⃣ classic version: counter = 0 for c in input(): if c in 'aeiouAEIOU': counter += 1 print(counter) 2️⃣ shorter version: print(len([c for c in input() if c in "AEIOU"])) 3️⃣ another version: print(sum([1 for c in input() if c in "AEIOU"]))
4th Mar 2021, 8:38 PM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
+ 3
Different ways to do it , https://code.sololearn.com/cZRfOzbdz9Vg/?ref=app I have timed each of those ways as well by running them 10,000 times , just to show you the performance of them.The final time is cumulative sum of running them that many times.
3rd Mar 2021, 9:54 PM
Abhay
Abhay - avatar
+ 2
Santiago Torres Busquets # not onelined, but quite shorter: import re inp = input() or 'HEllo wOrld' print(len(re.findall('[aeiou]',inp,re.I)),'vowels in "%s"'%inp)
3rd Mar 2021, 10:09 PM
visph
visph - avatar
+ 2
This worked for me. x = input() result = 0 for i in x: if i == "a" or i == "e" or i == "i" or i == "o" or i == "u": result += 1 print(result)
31st Aug 2021, 4:07 AM
Yuta Kato
+ 2
my answer in python: x=input() vowels="AEIOUaeiou" count=0 for i in x: if i in x in vowels: count+=1 print(vowels.count(x)) input: Leesi however, when I test, I get an output of zero. Can anyone spot what i'm writing incorrectly? I also wrote: x=input() vowels=["A", "E", "I", "O", "U", "a", "e", "i", "o", "u"] count=0 for i in x: if i in x in vowels: count+=1 print(vowels.count(x)) same input, "Leesi", out output is still zero. I'm a bit confused.
18th Mar 2022, 1:35 AM
Shalisa Miller
+ 1
i = list(input()) vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"] count = 0 for x in vowels: count += i.count(x) print(count)
27th Jun 2022, 4:54 PM
Martin Valdés Mallaug
+ 1
x = list(input()) cont_a = x.count("a") cont_e = x.count("e") cont_i = x.count("i") cont_o = x.count("o") cont_u = x.count("u") cont_A = x.count("A") cont_E = x.count("E") cont_I = x.count("I") cont_O = x.count("O") cont_U = x.count("U") total = cont_a + cont_e + cont_i + cont_o + cont_u print(total) #total1 = cont_A + cont_E + cont_I + #cont_O + cont_U #print(total1) #if total == list(input()): # print("it is vowel") #elif total != list(input()): #print("not a vowel") #if total == x: # print("it is vowel") #elif total != x: #print("not a vowel") #if total == list(input()): # print(total) #elif total != list(input()): #print(total) #if total == x: # print(total) #elif total != x: #print(total)
13th Nov 2022, 7:02 PM
Anita chaubey
Anita chaubey - avatar
+ 1
x = list(input()) cont_a = x.count("a") cont_e = x.count("e") cont_i = x.count("i") cont_o = x.count("o") cont_u = x.count("u") cont_A = x.count("A") cont_E = x.count("E") cont_I = x.count("I") cont_O = x.count("O") cont_U = x.count("U") total = cont_a + cont_e + cont_i + cont_o + cont_u print(total) #total1 = cont_A + cont_E + cont_I + #cont_O + cont_U #print(total1) #if total == list(input()): # print("it is vowel") #elif total != list(input()): #print("not a vowel") #if total == x: # print("it is vowel") #elif total != x: #print("not a vowel") #if total == list(input()): # print(total) #elif total != list(input()): #print(total) #if total == x: # print(total) #elif total != x: #print(total)
13th Nov 2022, 7:02 PM
Anita chaubey
Anita chaubey - avatar
+ 1
Hi everybody! My code : a = input() a = a.lower() vowels = 'aeiou' count = 0 for x in a : if x in vowels : count += 1 print(count)
16th Mar 2023, 7:04 AM
Amir Hossein Baghban
Amir Hossein Baghban - avatar
+ 1
Ed Theone Avoid using nested loops when coding, it makes the code more complex. Also, why don't you use "for letter in word.lower()" instead? It will make the code more readable and easier to code. Alternative code for you: word = input() count = 0 for letter in word.lower(): if letter in "aeiou": count+=1 print(count) #This code creates a for loop that loop through every lowercase'd character of the given string 'word', checks if the character is in "aeiou", then increases count by 1 if True. Then, print out the count var. #The difference between this code and your code is, we added the 'lower()' function in the for loop, we removed the 'lower()' function in line 5(if letter in "aeiou"), we then remove the 2 else(s) statement. The 'count = count' one will assign the variable with itself, so it DOES NOT change anything to the count var if the 'if' statement condition is False. #These tips will help you a lot when you code in any languages. There are many other tips and advice when coding.
17th May 2023, 12:07 PM
Dragon RB
Dragon RB - avatar
+ 1
Thanks for advice, Dragon RB! I'll use it for sure!)
18th May 2023, 5:31 PM
Ed Theone
Ed Theone - avatar
+ 1
My solution: vowel_s = ['a','e','i','o','u','A','E','I','O','U'] xWord = input() spltXWord = list(xWord) t = 0 vovl = 0 for y in spltXWord: if y in vowel_s: vovl += 1 print(vovl) https://code.sololearn.com/cxYzpq4rR0Ju/?ref=app
28th May 2023, 7:15 AM
IsantosDowd
IsantosDowd - avatar
0
I actually figured this out. The nested if inside the foor loop was not allowing the count to increase. New code: For i in x: If i in vowels: count+=1 Print(vowels.count(x))
18th Mar 2022, 2:16 AM
Shalisa Miller
0
And pythontutor.com helped me see the code visualized as i was typing so it helped me debug as im a very visual learner in case this helps skmeone else.
18th Mar 2022, 2:17 AM
Shalisa Miller
0
x = list(input()) cont_a = x.count("a") cont_e = x.count("e") cont_i = x.count("i") cont_o = x.count("o") cont_u = x.count("u") total = cont_a + cont_e + cont_i + cont_o + cont_u print(total)
29th May 2022, 1:20 PM
Guruprasad Damodhar
Guruprasad Damodhar - avatar
0
# your code goes here text = input('Enter any text : ') vowels = [] for char in text.lower(): if(char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u'): vowels.append(char) else: pass print(len(vowels)) this is working in code editor but it's not accepting as answer.
30th May 2022, 4:35 PM
R.G.I.T.B Jayarathne
0
Shalisa Miller You can easily find an error in your code when you discover the difference between the "count" variable and the "count( )" method‼️🤔
19th Sep 2022, 3:25 PM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
0
i = input() vowels = ['a','e','i','o','u','A','E','I','O','U'] c=0 for x in vowels: if i.count(x) > 0: c += i.count(x) print(c)
25th Sep 2022, 7:38 AM
Gargi Chandrababu
Gargi Chandrababu - avatar
0
sen=input() vowels=["a","e","o","i","u","A","E","I","O","U"] a=[i for i in sen if i in vowels] print(len(a)) Try my new code Definitely it will work
28th Dec 2022, 1:33 PM
Laxmi Mehta
Laxmi Mehta - avatar
0
My solution: Say = input() Say.lower() P = ["a","e","i","o","u"] Tell = [x for x in say if x in p] print(len(Tell))
30th Apr 2023, 11:04 AM
Dragon RB
Dragon RB - avatar