The Correct Answer
Store the correct option number in a variable
Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.
The quiz displays a question and four options, but it has no record of which option is right. When the player answers, there is nothing to compare against. You need a variable that holds the correct answer.
Integers hold whole numbers
You've been working with strings so far. Integers store whole numbers — no quotes:
num_players = 4The difference matters. "4" (with quotes) is a string — the text "4". 4 (no quotes) is an integer — a number. You will compare the player's answer to correct later, so it needs to be an integer.
Storing the correct option
For this question, option 3 ("float") is the right answer. Store that as an integer:
winner = 2Once the player submits their answer, you will compare it to this value to determine whether they got it right.
Instructions
Add the correct answer variable.
- After the
option_4variable, create a variable namedcorrectand assign it the integer3— this records which option is right so the quiz can compare the player's answer against it later.
question = "What is the type of the value 3.14?"
option_1 = "int"
option_2 = "str"
option_3 = "float"
option_4 = "bool"
# Step 1: Create the correct variable
print("=== Python Quiz ===")
print()
print(question)
print()
print(f" 1) {option_1}")
print(f" 2) {option_2}")
print(f" 3) {option_3}")
print(f" 4) {option_4}")
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding