+ 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!
23 Respostas
+ 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"]))
+ 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
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)
+ 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)
+ 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.
+ 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)
+ 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)
+ 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)
+ 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)
+ 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.
+ 1
Thanks for advice, Dragon RB! I'll use it for sure!)
+ 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
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
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)
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
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))