How to count the number of vowels in a string without a function wrapper
<p>I've currently solving a MIT undergrad problem in Python 3.5.
The goal is to write a Python script counting and printing the number of vowels in a string containing only lower-case letters without using a function wrapper or even a function definition (stated in the assignment, weird ?).</p>

<pre><code>def vowels_count(s):
    i=0
    counter = 0
    while(s[i] != " "):
        if s[i] == "a" or s[i] == "e" or s[i] == "i" or s[i] == "o" or s[i]   == "u":
            counter += 1
        i = i + 1
    return(counter)
</code></pre>

<p>I have two problems:
1/ first of, my own code using a while do structure meets a problem with the use of the index navigating from the first character to the last one. The debugger says: index out of range
2/ finally, if I have to comply with the MIT instructions, I would not be able to do anything in a single-line code without defining a function. </p>

<p>Thanks for your support</p>

<p>Why is this version not correct on the string index i ?</p>

<pre><code>def vowels_count_1(s):
    i = 0
    counter = 0
      while(s[i] != ""):
        if s[i] == "a" or s[i] == "e" or s[i] ==  "i" or s[i] == "o" or s[i] == "u":
          counter += 1
      i += 1
    print("Number of vowels: " + str(counter))
</code></pre>

<p>As you learn more and more you'll be able to count the vowels in one line using <code>sum</code> and a generation expression.</p>

<p>You could fix your loop <code>while i &lt; len(s)</code>, i.e. up to the length of the string, but much better is just to <em>iterate</em> over the sequence of characters we call "string".</p>

<pre><code>for ch in s:
    if ch == 'a' or ...
</code></pre>

<p>No indices needed. No <code>i</code>.</p>

<p>If you have learned the <code>in</code> operator already, you could simplify the test.</p>

<hr>

<p>Without a function probably means this:</p>

<pre><code>s = "the string"
# your code here
print("vowel count:", counter)
</code></pre>

<p>But I'm not sure ...</p>

python string