Phyton 3.5 "for loop" "enumerate()â Select Word from List
<p>Hi I'm new here I've just started learning python</p>

<p>Individual words in wordList will be referred to as âwordâ as a variable.</p>

<p>who would I set this up this for a python english grammar gen program by the way. </p>

<p>also I wanna try  to Enter a loop that repeats once for each word in wordList. Trying to use a for loop with âenumerate()â function but I keep getting stuck. Any help would be great. </p>

<pre><code>print('Welcome to my English Test App')



# Import the random module to allow us to select the word list and questions at random.
import random
candidateWords = ['HELLO', 'GOODBYE', 'NAME', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'BIG', 'SMALL', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'PIGEON', 'POSTER', 'TELEVISION', 'SPY', 'RHYTHM', 'SUBSTANTIAL', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
# places words down in a list
s = random.sample (candidateWords, 5)
for index,w in enumerate(s):
print(index,w)
</code></pre>

<p>if your question is how to use enumerate, here's an application for your code sample:</p>

<pre><code>import random
candidateWords = ['HELLO', 'GOODBYE', 'NAME', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'BIG', 'SMALL', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'PIGEON', 'POSTER', 'TELEVISION', 'SPY', 'RHYTHM', 'SUBSTANTIAL', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']

s = random.sample (candidateWords, 5)

# loop through the list using enumerate

for index,w in enumerate(s):
    print("word {} out of {} is {}, nb vowels is {}".format(index+1,len(s),w,countVowels(w)))
</code></pre>

<p>output</p>

<pre><code>word 1 out of 5 is NIGHT, nb vowels is 1
word 2 out of 5 is BALLOON, nb vowels is 3
word 3 out of 5 is SUBSTANTIAL, nb vowels is 4
word 4 out of 5 is BIG, nb vowels is 1
word 5 out of 5 is PIGEON, nb vowels is 3
</code></pre>

<p>Note that you don't have to use enumerate if you don't need the indexes:</p>

<pre><code>for w in s:
    print(w,countVowels(w))
</code></pre>

<p>for your count vowels (since everyone seemed to think this was the question at some point), you can do this, and whether Y is a vowel or not is up to you since it's unclear (<a href="http://www.oxforddictionaries.com/words/is-the-letter-y-a-vowel-or-a-consonant" rel="nofollow">Oxford</a>)</p>

<pre><code>def countVowels(word):
    return sum(word.count(x) for x in "AEIOUY")
</code></pre>

<p>The generator comp will apply count characters for each vowels in the word, then applies standard sum function.</p>

python python-3.x for-loop enumerate