The statement:
while i in s:
does not do what you think. Were that while
a for
, it would iterate over the string one character at a time, and probably work.
However, the expression i in s
(which is what that is in a while
statement) simply checks if i
is one of the things in the s
"collection" and gives you true or false. It does not iterate i
over the s
collection.
If i
had been set to something, the while loop would either execute infinitely or never, depending on the value of i
. If i
is not bound to a value, you'll get a run-time error.
As a solution, you can iterate over the characters in a string with something like (from an actual transcript):
>>> str = "pax"
>>> for ch in str:
... print(ch)
...
p
a
x
The equivalent while
version would be:
>>> str = "pax"
>>> idx = 0 # OR: idx, slen = 0, len(str)
>>> while idx < len(str): # while idx < slen:
... print str[idx]
... idx += 1
...
p
a
x
though the for
variant is generally considered more Pythonic for this sort of task.
Further, you can detect if a character is one of a set of characters by using in
, such as in the following transcript:
>>> str = "pax"
>>> for ch in str:
... if ch in "ABCabc":
... print(f"{ch} is either a, b, or c")
...
a is either a, b, or c
So you should be able to combine that for/while
loop and if
statement to count the vowels and output it (with print
).
Note especially the string I use for vowel checking, it also contains the upper-case vowels. And keep in mind, though your specification may only be to use the Latin-ish vowels, the Unicode world of today would not forgive this oversight. See here for example.