Subscribe to the Islander Newsletter

Wise choice future islander! Nuggets of gold shall be sent your way!
Email address
Secure and Spam free...
The Ultimate Step-by-Step Tutorial for Creating a Multiple Choice Quiz in Python

The Ultimate Step-by-Step Tutorial for Creating a Multiple Choice Quiz in Python

Do you want to know how to create a back-end multiple choice quiz in Python?

If so, then read on to know the exact steps required to make the quiz, whether it’s for a school assignment, curiosity or for anything else!

Let’s dive in!

How to Make the Quiz

The Ultimate Step-by-Step Tutorial for Creating a Multiple Choice Quiz in Python

The first thing you want to do is store the answers in a list or a tuple depending on your preference. You have to put the quotation marks around each answer in order to make them a string without any errors.

Next, you want to create a different list to store the questions in. Each question will be in their own strings. Each answer within these questions will be spaced out with the \n command. Below is a picture of the code typed in to ensure the correct format of the questions when they print out:

The Ultimate Step-by-Step Tutorial for Creating a Multiple Choice Quiz in Python

The Ultimate Step-by-Step Tutorial for Creating a Multiple Choice Quiz in Python  (continued from Green in question 3)

As you can see, there are upper and lowercase answers to the questions. This is to account for the possibility that the user can input the answer one way or the other and still get the question correct. The for loop will reflect this.

Eventually, we are construct a for loop that will go through each of these questions and allow the user to input an answer to the questions when they show up on the screen when they run the program. We will compare these answers to the index of the corresponding answer to the question.

The next thing we’ll do is assign variable k to be equal to -1 (k = -1). This is done so we can add 1 to k each time we go into the for loop to match the corresponding index of the answer we desire from the answers list.

We then want to store the number of questions into its own variable. The number of questions we have is 3, so we will store it as “number_of_questions = 3.” This will be useful in the print statement when we tell the user how many questions they got correct out of the number of questions they answered.

The next variable we want to create is the variable for how many question the user got correct (questions_correct = 0). This will start as zero because they haven’t gotten anything correct yet. However, if the user gets a question correct, we will use an if statement to add 1 to this variable each time the user’s answer matches the answer stored in the corresponding index for the question.

The code for the last 3 points goes as follows:

The Ultimate Step-by-Step Tutorial for Creating a Multiple Choice Quiz in Python

Now we will construct the for loop so the quiz can execute.

The first thing we will write here is “for m in questions.” What this is saying in English is this: “For each item in the questions list, I want you to do the following.” The m refers to each index in the questions list. Since this is a for loop, Python will go into the questions list and run the for loop for each index in there.

The next thing we’ll write is the answer variable. Now the answer variable will have the input for the user’s answer for each questions. This will get written as “answer = input(m).” It will work because the for loop is using m to refer to each string in the questions list. It will take the string it’s on and store it in input for the user to answer the question.

For instance, if Python was in the first question, m would be “What color are strawberries?” It will take this question in the format we wrote it in and store it in the input for the user to then answer the question. As you will see, the answer the user gives us will compared to the correct answer we assigned to the question we are asking them.

Now do you remember the k value we assign earlier (k = -1)? We will use this to send k to 0 to see if the first question is correct with k = k + 1. This reassigns k to be zero when Python goes through the first question. In the if-else statement that follows, we will compare the answer the user gave us to the answer in index zero of the answers list we defined earlier.

The code for the first 3 lines of the for loop goes as follows:

The Ultimate Step-by-Step Tutorial for Creating a Multiple Choice Quiz in Python

The next part of the code accounts for what will happen if the user answers each question correctly or incorrectly.

We will first write an if statement to tell the program what to do if they answer the question correctly.

So I encourage you to following down in Python and on a sheet of paper before I explain what this means and what Python is doing:

The Ultimate Step-by-Step Tutorial for Creating a Multiple Choice Quiz in Python

The if statement takes each answer the user gave us and it compares them to the corresponding k index of the answers list. In this case, we will compare the answer to each of the questions to index 0 for the 1st run of the for loop (question 1), index 1 for the 2nd run of the for loop (question 2) and index 2 for the 3rd run (question 3).

If the answer for, say, the question in index 0 of questions is a character in the string for index 0 of the answers list, then we will add one to the questions_correct variable to indicate how many questions the user has gotten correct thus far.

Just to remind you, each question is item m in the for loop since m is indicating the terms that we want the for loop to pass through when we go through the questions list.

Now for else, let’s say for the 1st question, I answered b. Python will first check to see if the condition in the if statement is true. In this case, answers[0] = “Aa.” Python will go through each character in answers[0] to see if b is a character in answers[0]. Since b isn’t one of the characters, Python will not run the if statement for that answer and will proceed to the else statement and run what’s in it.

In this case, the prompt in the else statement is questions_correct = questions_correct, which tells Python to keep questions_correct the same value that it is right now. We don’t put an actual value like 0 or 1 next to this questions_correct variable because the questions_correct variable already contains the number of questions that are correct.

For instance, if questions_correct is currently 2, then questions_correct = questions_correct means “Keep questions_correct at 2. Don’t change it.”

Do you understand? Comment below with any questions you have. I’ll be more than happy to clarify anything to you.

If so, here’s the last statement after the entire questions list is gone through to tell the user how many questions they got correct out of the number of questions they were asked:

The Ultimate Step-by-Step Tutorial for Creating a Multiple Choice Quiz in Python

This is a concatenation that will use the string command and convert the number of questions correct and the number of questions on the quiz from numbers to strings.

The output will be:

The Ultimate Step-by-Step Tutorial for Creating a Multiple Choice Quiz in Python

In this case, we got 3 questions correct, so questions_correct = 3 and the total number of questions was 3, so number_of_questions = 3.

If we got 2 questions correct, the output format would be the same except questions_correct = 2, so the output is this:

The Ultimate Step-by-Step Tutorial for Creating a Multiple Choice Quiz in Python

And so on if the user gets 1 question correct or none of the questions correct.

Did this tutorial make sense?

The entire code is as follows:

The Ultimate Step-by-Step Tutorial for Creating a Multiple Choice Quiz in Python

To recap, we stored the correct answers in a list that will be compared to the answers the user inputs each time we run the question indexes in the for loop.

The questions list is written to run in the for loop each time we need an answer to those questions.

k = -1 so we can redefine it each time we enter the for loop to match the answers index.

The number_of_questions variable is defined to use in the last print statement to tell the user how many questions they got correct out of the number of questions on the quiz.

The questions_correct variable is initially defined as zero so we can change it if the user gets any number of questions correct.

The for loop uses the m term (you don’t necessarily have to define this as m; it can be whatever you want) to refer to each item in the questions list. It says “For each term in the questions list, do this.”

The input(m) command says “Tell the user the m item in the questions list and the answer they return to us will be assigned to the answer variable.”

K is then redefined each time we go into the for loop to match the index in answers that we want to compare the answer the user gave us to.

The if statement says “If the answer the user gave us for this run is in the k index of answers, add 1 to the questions_correct variable.”

The else says “If the if statement is not true, leave questions_correct the way it is now.”

The print statement converts all questions_correct and number_of_questions to strings to print out the number of questions the user got correct.

And that is how you construct a back-end multiple choice quiz in Python!

If you do have any questions whatsoever, don’t be afraid to ask in the comment below or to shoot me an email at jointheisland@gmail.com. I am more than happy to repeat the entire post again to you if you want.

Until next time,

This is Evan signing off.

PS: If you got a lot out of this tutorial and you want more Python tutorials along with the world’s best posts on productivity and reaching your potential in school and your career sent straight to your inbox, definitely subscribe with your email to Join the Island in the action box below!

 

 

 

Evan Cruz
+ posts

Evan Cruz is the founder of Join the Island, the website committed to helping young adults become massively productive and reach their full potential.

He has been featured on Vox, OnlineU, and UpJourney. He has also a cited human relations expert and college expert.

He graduated Magna Cum Laude with a Bachelor of Science Degree in Civil Engineering.

Read more about Evan and Join the Island here.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Islander Newsletter

Wise choice future islander! Nuggets of gold shall be sent your way!
Email address
Secure and Spam free...