+ 2

This code is not working.Can anyone help me? I have to count no. of vowels in the string

s="rams cingulate services " x=len(s) count=0 a=0 while a<=x: if s[a]=="a" or s[a]=="e" or s[a]=="i" or s[a]=="o" or s[a]=="u": count=count+1 a=a+1 print("count",count)

2nd Jul 2019, 2:49 PM
Kirti Mishra
Kirti Mishra - avatar
2 Réponses
+ 21
a = a + 1 needs to be indented. Otherwise it's not in the loop and you're creating an infinite loop. ~~~ Btw, a more "pythonic" way to get the same result would be: count=0 for c in s: if c in 'aeiou': count += 1 Your code isn't wrong. But you're basically writing a C code with Python syntax
2nd Jul 2019, 3:52 PM
Anna
Anna - avatar
+ 3
Replace a<=x by a<x. Don't forget list index starts at 0... And ends at len(list) - 1.
2nd Jul 2019, 3:20 PM
Théophile
Théophile - avatar