for-loop Python 3.5. Creating a quiz for students I tutor
<p>Hello I'm currently trying to make a quiz application for year english students. I'm using Python 3.5. and I'm kinda new. The quizz is supposed to work like below I'll include the code as well. </p>

<blockquote>
  <p>WELCOME TO YOUR QUIZ</p>
  
  <p>Word 1/5: Potato How many consanants does the word contain?</p>
  
  <p>3</p>
  
  <p>Correct!</p>
  
  <p>Word 2/5: Potato How many vowels does the word contain?</p>
  
  <p>1</p>
  
  <p>Correct!</p>
  
  <p>Word 3/5: Name How many vowels does the word contain</p>
  
  <p>5</p>
  
  <p>Incorrect! Correct answer 4 </p>
  
  <p>Word 4/5: YES How many letters does the word contain? 3 Correct!</p>
  
  <p>Word 5/5: Day</p>
  
  <p>What is letter 3 of the word?</p>
  
  <p>Y</p>
  
  <p>Correct!</p>
  
  <p>Game Over. Your Score is 4/5</p>
</blockquote>

<pre><code>print('WELCOME TO YOUR QUIZ')

# Import the random module to allow us to select the word list and questions at random.
import random
quizWords = ['WOMBAT', 'COMPUTER', 'BOOKS', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'HUGE', 'TINY', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'KOOKARBURRA', 'POSTER', 'TELEVISION', 'PRINCE', 'RHYTHM', 'SUDDENLY', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
s = random.sample (quizWords, 5)
# loop through the list using enumerate 
for index,w in enumerate(s):
    print("Word {}/{}:{}".format(index+1,len(s),w))  
</code></pre>

<p>I want to generate a random number between 1 and 4, and use it to choose which question is asked for each word. </p>

<p>If the random number is 1, I want to ask the user âHow many letters does the word contain?â and prompt them for input. Assess their answer, and then print an appropriate message. How would i fit that in?</p>

<p>I'm proposing a way to select a question, and associate a test function, which allows to define as many questions you want easily:</p>

<pre><code>print('WELCOME TO YOUR QUIZ')

# Import the random module to allow us to select the word list and questions at random.
import random
quizWords = ['WOMBAT', 'COMPUTER', 'BOOKS', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'HUGE', 'TINY', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'KOOKARBURRA', 'POSTER', 'TELEVISION', 'PRINCE', 'RHYTHM', 'SUDDENLY', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
s = random.sample (quizWords, 5)

def test_nb_vows(word):
    return sum(word.upper().count(x) for x in "AEIOUY")


qt = {"How many vowels does the word contain?" : test_nb_vows, "How many letters does the word contain?" : len}

# loop through the list using enumerate
for index,w in enumerate(s):
    print("Word {}/{}:{}".format(index+1,len(s),w))
    # check question
    qk = random.choice(list(qt.keys()))
    answer = int(input(qk))
    real_answer = qt[qk](w)
    if real_answer == answer:
        print("Correct!")
    else:
        print("Noooo it is {}".format(real_answer))
</code></pre>

<p>The idea is to choose the question at random using <code>random.choice</code>. The choice is a key to a dictionary, and the value is the test function, which must have 1 parameters: the word itself, and returns the correct answer (restricted to a number right now), which is tested against user input</p>

<p>That way you can define a lot of questions as long as you provide the relevant test function. You just have to enrich <code>qt</code> and code the test function (or use existing functions like I did by reusing <code>len</code>). Good luck.</p>

python python-3.x for-loop