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 in your first question. If you haven’t coded in Python before, there are many courses and resources out there where you can learn Python for free.

Create a new text file called “InteractiveQuiz. py”. Open the file using 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. “) Ask the first question to the user. Use the input() function to wait for the user’s response, and store their input into the variable “userInput”. print(“Question 1. What city is the capital of Australia?")userInput = input() Add a condition to check if the user’s input matches the correct answer. If the user answered correctly, display a “correct” message to them. If not, display the correct answer. if (userInput. lower() == “Canberra”. lower()):    print(“That is correct!")else:    print(“Sorry, the correct answer is Canberra. “) To run your quiz and test that your question is working, open the command line and navigate to the location of your Python file. For example, if you stored your file in a directory named Desktop, the command would be: cd Desktop Run the python command to execute the quiz. python InteractiveQuiz. py Provide an answer to the quiz question.

How to Add Multiple Questions to the Quiz

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

At the top of the Python file, add a class to store information about a quiz question. If you have not done this before, you can learn more about how to create 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 +’}’ Underneath the class, add an array of question objects. These objects will 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”)] Replace the existing if statement and user input code. Instead, use a for loop to iterate over the quizQuestions array. For each question, display the question, and compare the user’s input with 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 expand the Question class to accommodate multiple-choice questions.

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) +’}’ Add another question to the quizQuestions array. Store 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”]),] Modify the part of the for loop that displays the question to the user. If multiple-choice options exist for the question, display them after the question, and before fetching the user’s 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 That Has Alternate Correct Answers

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

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

Add another optional attribute to the Question class. This attribute will contain any possible alternate 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) +’}’ Add another question to the quizQuestions array. Add “Northern Hemisphere” as the correct answer. Add “north” and “northern” as alternate correct answers. quizQuestions = [    #. . .     Question(“Question 5. What hemisphere is Japan located in”, “Northern Hemisphere”, [], [“north”, “northern”]),] Add another condition to the if statement that checks if the user has entered an alternative 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 the User Hints

You can modify the script so that the user is unable to progress to the next stage until they get the current question correct. In this case, add a variable to count how many times the user has entered a wrong answer. After three incorrect guesses, you can give the user a hint.

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) +’}’ Add hints to all the 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”]),] Remove the if statements that check if the user answered the question correctly. Replace these with a while loop. The while loop will continuously loop until the user gets the answer correct. Inside the while loop, add a count that will display the hint once the user gets the answer wrong 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!”) Rerun your quiz in the command line using the python command. python InteractiveQuiz. py Answer questions to the quiz.

Creating Small Applications Using Python

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

You can store information about the quiz questions in an object, to prevent you from repeating your code for each question.

To improve your skills in Python, it is a good idea to practice different exercises and examples. You can have a look at other Python examples that can help you learn.