How to Make an Interactive Quiz Game in Python

Stuck on a simple programming challenge? Try writing your own quiz to test friends and family; it’s easy in Python.


You can practice learning to program by creating short command-line projects in Python. One of the projects you can create is an interactive quiz where you can ask the user different types of questions.


Questions you can ask include multiple choice questions or questions that require written answers. You can also ask questions that have alternate correct answers. You can even make your program give hints to the user.


How to create the Python script and add your first quiz question

Create a Python file and add your first question. If you have never programmed in Python before, there are many courses and resources where you can learn Python for free.

  1. Create a new text file called “InteractiveQuiz.py”.
  2. Open the file with any text editor and add your first print statement to welcome the user to the quiz.
    print("Hello, welcome to the geography quiz. Answer the questions as they are presented.")
  3. Ask the user the first question. Use the input() function to wait for the user’s response and store their input in the userInput variable.
    print("Question 1. What city is the capital of Australia?")
    userInput = input()
  4. Add a condition to check if the user’s input matches the correct answer. If the user answered correctly, show them a “correct” message. If not, indicate the correct answer.
    if (userInput.lower() == "Canberra".lower()):
    print("That is correct!")
    else:
    print("Sorry, the correct answer is Canberra.")
  5. To run your quiz and test that your question works, open the command line and navigate to your Python file location. For example, if you saved your file in a directory called Desktop, the command would be:
    cd Desktop
  6. Run the Python command to run the quiz.
    python InteractiveQuiz.py
  7. Provide an answer to the quiz question.

How to add multiple questions to the quiz

You can add multiple questions by repeating the code above. However, this will make your code unnecessarily long and more difficult to update. For a better approach, store information about the question in an object instead.

  1. Add a class at the top of the Python file to store information about a quiz question. If you haven’t already, you can learn more about creating a class in Python.
    class Question:
    def __init__(self, questionText, answer):
    self.questionText = questionText
    self.answer = answer

    def __repr__(self):
    return '{'+ self.questionText +', '+ self.answer +'}'

  2. Add an array of question objects under the class. These objects store the question text that the quiz displays to the user, along with the correct answer.
    quizQuestions = [
    Question("Question 1. What city is the capital of Australia", "Canberra"),
    Question("Question 2. What city is the capital of Japan", "Tokyo"),
    Question("Question 3. How many islands does the Philippines have", "7100")
    ]
  3. Replace the existing if statement and user input code. Instead, use a for loop to iterate through the QuizQuestions array. For each question, view the question and compare the user’s input to the correct answer.
    for question in quizQuestions:
    print(f"{question.questionText}?")
    userInput = input()
    if (userInput.lower() == question.answer.lower()):
    print("That is correct!")
    else:
    print(f"Sorry, the correct answer is {question.answer}.")

How to add multiple choice questions

You can extend the Question class to include multiple choice questions.

  1. Modify the Question class at the top of the file. Add an optional attribute called multipleChoiceOptions.
    class Question:
    def __init__(self, questionText, answer, multipleChoiceOptions=None):
    self.questionText = questionText
    self.answer = answer
    self.multipleChoiceOptions = multipleChoiceOptions

    def __repr__(self):
    return '{'+ self.questionText +', '+ self.answer +', '+ str(self.multipleChoiceOptions) +'}'

  2. Add another question to the QuizQuestions array. Save some multiple choice options for the question.
    quizQuestions = [
    Question("Question 1. What city is the capital of Australia", "Canberra"),
    Question("Question 2. What city is the capital of Japan", "Tokyo"),
    Question("Question 3. How many islands does the Philippines have", "7100"),
    Question("Question 4. Which country takes the most land mass", "b", ["(a) United States", "(b) Russia", "(c) Australia", "(d) Antarctica"]),
    ]
  3. Change the part of the for loop that displays the question to the user. If the question has multiple choice options, show them after the question and before getting user input.
    for question in quizQuestions:
    if (question.multipleChoiceOptions != None):
    print(f"{question.questionText}?")
    for option in question.multipleChoiceOptions:
    print(option)
    userInput = input()
    else:
    print(f"{question.questionText}?")
    userInput = input()

    if (userInput.lower() == question.answer.lower()):
    print("That is correct!")
    else:
    print(f"Sorry, the correct answer is {question.answer}.")

How to add a question with alternative correct answers

Sometimes there are questions where the user can type part of the answer, but it’s still technically correct.

For example, one of the questions in your quiz might be “What hemisphere is Japan in?”. In this case, the user can enter “North”, “Northern” or “Northern Hemisphere” and still be correct.

  1. Add another optional attribute to the Question class. This attribute contains all possible alternative correct answers that the user can enter.
    class Question:
    def __init__(self, questionText, answer, multipleChoiceOptions=None, alternateAnswers=None):
    self.questionText = questionText
    self.answer = answer
    self.multipleChoiceOptions = multipleChoiceOptions
    self.alternateAnswers = alternateAnswers

    def __repr__(self):
    return '{'+ self.questionText +', '+ self.answer +', '+ str(self.multipleChoiceOptions) +', '+ str(self.alternateAnswers) +'}'

  2. Add another question to the QuizQuestions array. Add “northern hemisphere” as the correct answer. Add “North” and “North” as alternate correct answers.
    quizQuestions = [
    Question("Question 5. What hemisphere is Japan located in", "Northern Hemisphere", [], ["north", "northern"]),
    ]
  3. Add another condition to the if statement that checks if the user entered an alternate correct answer.
    if (userInput.lower() == question.answer.lower()):
    print("That is correct!")
    elif (question.alternateAnswers != None and userInput.lower() in question.alternateAnswers):
    print("That is correct!")
    else:
    print(f"Sorry, the correct answer is {question.answer}.")

How to give hints to the user

You can change the script so that the user cannot proceed to the next stage until they correctly ask the current question. In this case, add a variable to count the number of times the user entered an incorrect answer. After three wrong guesses, you can give the user a hint.

  1. Modify the Question class to use a new hint attribute.
    class Question:
    def __init__(self, questionText, answer, hint=None, multipleChoiceOptions=None, alternateAnswers=None):
    self.questionText = questionText
    self.answer = answer
    self.hint = hint
    self.multipleChoiceOptions = multipleChoiceOptions
    self.alternateAnswers = alternateAnswers

    def __repr__(self):
    return '{'+ self.questionText +', '+ self.answer +', '+ self.hint +', '+ str(self.multipleChoiceOptions) +', '+ str(self.alternateAnswers) +'}'

  2. Add hints to all questions in the quiz.
    quizQuestions = [
    Question("Question 1. What city is the capital of Australia", "Canberra", "Starts with a C"),
    Question("Question 2. What city is the capital of Japan", "Tokyo", "Starts with a T"),
    Question("Question 3. How many islands does the Philippines have", "7100", "A number between 7000 and 8000"),
    Question("Question 4. Which country takes the most land mass", "b", "The country spans two continents", ["(a) United States", "(b) Russia", "(c) Australia", "(d) Antarctica"]),
    Question("Question 5. What hemisphere is Japan located in", "Northern Hemisphere", "The top half of Earth", [], ["north", "northern"]),
    ]
  3. Remove the if statements that check if the user answered the question correctly. Replace this with a while loop. The while loop loops continuously until the user gets the correct answer. Inside the while loop, add a counter that displays the hint once the user has answered incorrectly three times.
    for question in quizQuestions:
    if (question.multipleChoiceOptions != None):
    print(f"{question.questionText}?")
    for option in question.multipleChoiceOptions:
    print(option)
    userInput = input()
    else:
    print(f"{question.questionText}?")
    userInput = input()
    count = 0
    while (userInput.lower() != question.answer.lower()):
    if (question.alternateAnswers != None and userInput.lower() in question.alternateAnswers):
    break;
    count = count + 1
    if (count >= 3):
    print(f"Hint: {question.hint}.")
    userInput = input()
    else:
    print("That is not correct, try again.")
    userInput = input()

    print("That is correct!")

  4. Rerun your quiz on the command line using the Python command.
    python InteractiveQuiz.py
  5. Answer questions on the quiz.

Building small applications with Python

One of the projects you can create while learning how to code in Python is an interactive quiz. In the quiz, you can ask the user different types of questions.

You can store information about the quiz questions in an object to avoid repeating your code for each question.

To improve your knowledge of Python, it’s a good idea to practice various exercises and examples. You can check out other Python examples that may help you learn.

Read  how to access free support in the UAE

Leave a Comment

Your email address will not be published. Required fields are marked *