+ 1

How to code "How many vowels in this strings"

I'm a novice about Python. I tried to code "Count how many vowels in this strings" There is a way I can code, but it's so messy so I wanted to make it simple. Below is the problem that I have to solve. """ Write a program that counts up the number of vowels contained in the string s. Valid vowels are: 'a', 'e', 'i', 'o', 'u' . For example, if s ='ffsdfddadxcxvxaaed', your program should print >>> Number of vowels: 4 So below is code that I coded. --------------------------------------------- s = 'dfsjknsckmlaaeiodkldmf' for i in s: vowels = 0 if i == 'a': vowels += 1 else: pass print(vowels) -------------------------------- When I run this, the result is 0. My question is, 1. why my coding result is 0? And what is wrong with my code? 2. How can I code simply about "How many vowels in this strings?" Thank you for making time and I hope you to have really great days!! :):):)

5th Jun 2020, 5:10 AM
beanibeanibean
beanibeanibean - avatar
23 Respostas
+ 7
using list: text = input() my_list = ["a", "e", "i", "o","u"] new_list = [] for c in text: if c in my_list: new_list.append(c) print(len(new_list))
13th Jun 2021, 1:00 PM
NAJOUA ZEGRITTY
NAJOUA ZEGRITTY - avatar
+ 5
text = input() count = 0 vowels = ["a", "e", "i", "o", "u"] for x in text: if (x in vovels): count += 1 print(count)
6th Jul 2021, 8:55 PM
Barış U.
Barış U. - avatar
+ 4
Try this. This works. str = input() count = 0 for c in str : if(c=='a' or c=='e' or c=='i' or c=='o' or c=='u' or c=='A' or c=='E' or c=='I' or c=='O' or c=='U'): count += 1 print(count)
22nd May 2021, 8:34 PM
W.W.A. Gayan Malinda
+ 3
전민경 Because you are re-initialise the vowels variable inside loop. That's why your result is giving 0. It should be like this: string = "abcdaefghaj" vowels = 0 for i in string: if i == 'a': vowels += 1 print(vowels)
5th Jun 2020, 5:13 AM
A͢J
A͢J - avatar
+ 3
Thank you so much! I did such a baby mistake uuuu Thanks a lot have a nice day!!
5th Jun 2020, 10:22 AM
beanibeanibean
beanibeanibean - avatar
+ 3
Here is my solution ,i think this is more shorter than what was posted here : string=input() count=0 for c in string : if c in {"a","e","i","o","u"}: count+=1 print(count)
21st Sep 2021, 2:22 PM
Hasnae BOUHMADY
Hasnae BOUHMADY - avatar
+ 2
Here is my solution: text = input() my_list = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U",] i = 0 for ch in text: if ch in my_list: i += 1 print(i)
8th Aug 2021, 11:58 AM
Masoud Ramezani
Masoud Ramezani - avatar
+ 2
str = input() count = 0 for c in str : if(c=='a' or c=='e' or c=='i' or c=='o' or c=='u' or c=='A' or c=='E' or c=='I' or c=='O' or c=='U'): count += 1 print(count)
20th Nov 2021, 11:47 AM
Stefan Bartl
Stefan Bartl - avatar
+ 2
print(len([x for x in input().lower() if x in ["a","e","i","o","u"] ]))
15th Dec 2021, 2:09 AM
Samnith Vath
Samnith Vath - avatar
0
Here is the code i tried but it keeps given me 0 string = () vowels = 0 for i in string: if i == 'a': vowels += 1 print(vowels)
12th Apr 2021, 7:00 AM
Simisola Osinowo
Simisola Osinowo - avatar
0
Simisola Osinowo You forgot to add 'input' at the beginning of your code. --- string = input() --- Also, you might want to include all the vowels and use 'in' instead of '=='. --- if i in 'aeiou': --- https://code.sololearn.com/cEBgsEN1Vw37/?ref=app
16th Apr 2021, 2:41 PM
Khidr
0
Vowels program making #your code goes here s = str(input('')) vowels = 0 for i in s: if i == 'a': vowels += 1 if i == 'e': vowels += 1 if i == 'u': vowels += 1 if i == 'ü': vowels += 1 if i == 'o': vowels += 1 if i == 'ö': vowels += 1 if i == 'ı': vowels += 1 if i == 'i': vowels += 1 else: pass print(vowels)
22nd Jun 2021, 8:08 AM
Fatih Urgen
0
sample_text = input() vowels = "aeiou" total = 0 for letter in sample_text: counter = 0 while letter != vowels[counter]: counter += 1 if counter == 4: break if letter == vowels[counter]: total += 1 print(total)
23rd Jul 2021, 7:02 PM
Jesus Tolentino Ramirez
Jesus Tolentino Ramirez - avatar
0
string = input() vowels = 0 for i in string: if i == 'a' or i=='e' or i=='i' or i=='o' or i=='u': vowels += 1 print(vowels)
10th Sep 2021, 8:11 PM
guenaizi abdelkader
guenaizi abdelkader - avatar
0
x = input('enter a string:') v=['a', 'e', 'i', 'o', 'u'] s=0 for c in x: for l in range (0, len(v)): if c==v[l]: s+=1 print(s)
28th Jan 2022, 8:16 AM
Alcino Castelo
0
x = input('enter a string:') v=['a', 'e', 'i', 'o', 'u'] s=0 for c in x: for l in range (0, len(v)): if c==v[l]: s+=1 print(s)
28th Jan 2022, 8:16 AM
Alcino Castelo
0
This is my code: n = str(input()) from collections import Counter counter = Counter(n) a=(counter['a']) e=(counter['e']) i=(counter['i']) o=(counter['o']) u=(counter['u']) print(a+e+i+o+u) OR SIMPLER: n = str(input()) a=(n.count('a')) e=(n.count('e')) i=(n.count('i')) o=(n.count('o')) u=(n.count('u')) print(a+e+i+o+u)
15th Feb 2022, 6:30 PM
Martijn Den Breejen
Martijn Den Breejen - avatar
0
text= input() a=text.count('a') e=text.count('e') i=text.count('i') o=text.count('o') u=text.count('u') cont=a+e+i+o+u print(cont)
13th Apr 2022, 1:50 AM
Asmaa Nasr
0
#your code goes here z = "aeiou" y = input() b=0 for i in y: if i in z: b +=1 else: repr(b) print(b)
10th May 2022, 1:46 AM
anas safi
0
string=input() count=0 for c in string : if c in {"a","e","i","o","u"}: count+=1 print(count)
10th Nov 2022, 8:06 AM
Pooja Patel
Pooja Patel - avatar