Vowel counter | Sololearn: Learn to code for FREE!

0

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!

3/3/2021 8:59:56 PM

Santiago Torres Busquets

13 Answers

New Answer

+22

counter = 0 for c in input(): if c in 'aeiouAEIOU': counter += 1 print(counter)

+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.

+2

A short version looks like this https://code.sololearn.com/cMRxdxe5wOm4/?ref=app

+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)

+1

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)

0

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.

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))

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.

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)

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.

0

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)

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‼️🤔

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)