How to Create a Multiple Choice Quiz With Python 3

Do you want to learn how you can create your own quiz in Python? This easy to follow tutorial will help you code a quiz with any questions you want, use multiple choice answer options, and even generate a final score at the end! This is a great guide for beginners in Python programming, so feel free to write your own code as you go along or even mess around with my source code.

How to Start Coding a Quiz in Python

First, we need to make sure we know exactly what our quiz will require. For one, we will need the question text that the user is asked. We will also use a set of answers as multiple choice, and our script will need to store the correct answer so we can generate a final score at the end. We won’t need to import any libraries because we’re just using some very simple Python!

  • Array of questions & multiple choice answers
  • Each question should store the correct answer
  • Score variable that starts at 0
  • A couple arrays to loop through each question, and each question’s answer options

Questions Data

questions = {
    "What is 15*3?":{"correct":"a", "answers":[45,35,60,40,30]},
    "What is 17 * 2?":{"correct":"b", "answers":[43,34,36,32,30]},
    "What is 19 * 3?":{"correct":"e", "answers":[58,48,55,59,57]},
    "What is 22 * 7?":{"correct":"c", "answers":[168,152,154,156,161]}
}

We can start off with our questions data. As you can see, I have set it up so that each question is actually a key, which then corresponds to some information about our multiple choice options and the actual correct answer. It may look odd now, but I will explain the correct “a,b,c,d,e” options soon.

correct = 0
total = len(questions)

We need to make sure that we can give the user a final score at the end, so we use the len() function of Python to figure out how many questions we actually have. In this example, we have 4 total questions. We also set our correct variable equal to 0 to begin.

Answer Labels

Our answer labels need to be defined, so we can match up letter keys (a, b, c, d, e) to a corresponding multiple choice option. This isn’t the 100% most ideal way to store a correct answer, but I want our quiz to function much like an actual test in school. Technically we could save ourselves some lines of code if we just forced the user to input the actual raw answer, but this can be tricky when answers are not integers and are instead strings – especially if they have capitalization or punctuation in them.

anslabels = ['a','b','c','d','e']

Asking a Multiple Choice Question in Python

It’s now time to loop through all of our questions, so we’re going to use the item() functionality to separate our question data into keys and values.

for k, v in questions.items():
    print(k)

Because our questions are set up to have the question text be the KEY and the correct / multiple options be stored in the VALUE, we’ll need one more loop to actually display the questions to the user. For now though, our loop will just print out each question to the user on a separate line.

In this next block of code, we will start another loop that will print out each multiple choice options, along with the corresponding letter (a, b, c, d, or e).

for k, v in questions.items():
    print(k)
    x = 0
    for ans in v["answers"]:
        print(anslabels[x] + ": " + str(ans))
        x += 1

The text in bold is what we have added to our first loop. You can see that we are setting x = 0 so that we can always make sure the first answer label for each question is the anslabel’s first (0 key) letter (a). We then use x += 1 so that in the next loop, we get the next letter identifier.

We are printing out the answer letter identifier, followed by the multiple choice option. Here is what this looks like for the first question in our quiz.

What is 15*3?
a: 45
b: 35
c: 60
d: 40
e: 30

It’s easy and it looks and feels like a real quiz!

Checking User’s Answer

We can use input() to get the user’s answer (a, b, c, d, or e) and then check if it’s correct. I chose not to tell the user if they were right or wrong, but you could definitely do that if you want.

answer = input("\nType a, b, c, d, or e to select your answer. Then press ENTER.\n")
    if(answer == v["correct"]):
        correct += 1

I have used a basic instruction for the user to follow. They then type in the letter identifier for the answer they believe is correct and then they can press ENTER / RETURN key to submit it. We can then use our question data to see if the answer matches the correct answer (a, b, c, d, or e). If it is correct, we add 1 to our correct variable that we set to 0 in the beginning.

Calculate Quiz Score

The final part of our script must calculate the user’s final score. This is simple because we already know the total number of questions, and we know how many questions the user answered correctly!

score = str((correct / total) * 100) + "%"
print("Quiz Complete!\nYour score is " + score + " on this quiz.")

We make sure that our correct / total division math is multiplied by 100 so that decimals from the original division can be displayed as more familiar numbers. For example 2 correct answers out of 4 total questions is equal to 0.5, or one half. We multiply by 100 to make our decimal come to 50.0 – Then we make sure to convert this to a string so we can add the % symbol to the end and then use the score in a concatenated string to tell the user their final score.

Python 3 Quiz Source Code

You can watch the quiz script working & running below in this short GIF. You can also check out the source code below. I recommend altering the script and making your own tweaks to improve upon it, but feel free to use the code and change the questions/answers to your liking!

I have added in some \n newlines and information with print() so that the quiz runs well. Some of this was not mentioned specifically above because it’s not required to have a functioning quiz in Python. Enjoy!

questions = {
    "What is 15*3?":{"correct":"a", "answers":[45,35,60,40,30]},
    "What is 17 * 2?":{"correct":"b", "answers":[43,34,36,32,30]},
    "What is 19 * 3?":{"correct":"e", "answers":[58,48,55,59,57]},
    "What is 22 * 7?":{"correct":"c", "answers":[168,152,154,156,161]}
}
anslabels = ['a','b','c','d','e']
correct = 0
total = len(questions)
input("\nThank you for trying out William Passmore's Python 3 Quiz!\nPress ENTER to start!\n")
for k, v in questions.items():
    print(k)
    x = 0
    for ans in v["answers"]:
        print(anslabels[x] + ": " + str(ans))
        x += 1
    answer = input("\nType a, b, c, d, or e to select your answer. Then press ENTER.\n")
    if(answer == v["correct"]):
        correct += 1
    print("\n")
score = str((correct / total) * 100) + "%"
print("Quiz Complete!\nYour score is " + score + " on this quiz.")

How to Learn Python Online for Free

Learning Python 3 for free is easy to do if you know where to find good tutorials and information. You can check out these 4 Python Projects for Beginners or sign up to my favorite programming community, Code Forum. Create your account and go make an introduction to meet some fellow coders! Then you can check out the Python forum category or another programming section to start learning, or even sharing what you already know.