Quiz Program – Part 2: adding to the quiz

Option 5 – Repeating a question

This gets a little bit tricky.

To repeat a question we need to use the idea of repetition in our code. In particular, we need to keep repeating until they get the answer right.

BEWARE: If you use this on a question the user doesn't know the answer to they might get really bored and stop playing the quiz. I'm going to use it on a multiple choice answer because they should always be able to get to the right answer...

To repeat the code until an answer is right we need to use a while loop.

Code helper image
  1. Start with a line above the question that sets a new variable called correct to False. They don't have the answer right. We'll use this variable to organise the loop.

    The code for this is:  correct = False

    Note: the capital F on False is IMPORTANT

  2. Then we can add the loop code itself:
    while correct == False:

    This means, do the following all the time that correct is False

    Note: two  ==  signs

  3. Then you need to indent everything to do with the question by 4 spaces. This is really important. Everything to do with the questions needs to be indented
  4. Then add a line of code in the part where you say that the answer is right to change correct to True – they got the answer right

    The code here is:  correct = True

    The capital T on True is also IMPORTANT

  5. Now run the code to check it works

If when you run the code the loop keeps going forever and ever and you can get out of it press the Esc key at the top left of the keyboard. Then say Yes, you do want to "kill the program"

You can do this with any question. BUT make sure you put the  correct = False  line before the question.