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>Here's a possible solution:</p>

<pre><code># 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 f0(word):
    """ TODO """
    return True

def f1(word):
    """ TODO """
    return True

def f2(word):
    """ TODO """
    return True

def f3(word):
    """ TODO """
    return True

def f4(word):
    """ TODO """
    return True

questions = {
    0: {
        "title": "How many consanants does the word {0} contain?",
        "f": f0
    },
    1: {
        "title": "How many vowels does the word {0} contain?",
        "f": f1
    },
    2: {
        "title": "How many vowels does the word {0} contain",
        "f": f2
    },
    3: {
        "title": "How many letters does the word {0} contain?",
        "f": f3
    },
    4: {
        "title": "What is letter 3 of the word {0}?",
        "f": f4
    }
}


# loop through the list using enumerate
for index, w in enumerate(s):
    print("Word {}/{}:{}".format(index + 1, len(s), w))
    type_question = random.randint(0, 4)
    print("{}".format(questions[type_question]["title"].format(w)))
    questions[type_question]["f"](w)
</code></pre>

<p>As you can see, you just use random.randint to pick up a random question from the questions dictionary. Now you just need to complete the logic of function f0..f4 ;-) . Hope it helps!</p>

python python-3.x for-loop