Is the Answer Right?
Use a comparison operator to check if the player's answer matches the correct one
Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.
The quiz shows the player's answer number and the correct answer number side by side. But the player still has to compare them manually. PyQuiz should do that comparison and store the result so the program knows whether the answer was right.
Python's comparison operators compare two values and produce a Boolean — either True or False.
| Operator | Meaning |
|---|---|
== | equal to |
!= | not equal to |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
For example:
temperature = 72
is_warm = temperature >= 70
print(is_warm)TrueThe expression temperature >= 70 evaluates to True because 72 is greater than or equal to 70. That Boolean value is stored in is_warm.
A == comparison works the same way — it checks whether two values are exactly equal and produces True or False:
target = 5
guess = 3
matched = guess == target
print(matched)FalseStore the result of your comparison in a variable so you can use it later — to print feedback, drive other logic, or both.
Instructions
Add the comparison and update the result display.
- After
answer = int(answer_text), create a variable namedis_correctand assign it the result of comparinganswertocorrectusing==— storing the result in a variable means you can act on it later withif/elseto print "Correct!" or "Wrong!". - Replace
print(f"The correct answer is: {correct}")withprint(f"Correct: {is_correct}")— this shows the Boolean result directly so you can verify the comparison is working before adding conditional logic.
question = "What is the type of the value 3.14?"
option_1 = "int"
option_2 = "str"
option_3 = "float"
option_4 = "bool"
correct = 3
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}")
print()
answer_text = input("Your answer (1-4): ")
answer = int(answer_text)
# Step 1: Create is_correct by comparing answer to correct
print()
print(f"You answered: {answer}")
# Step 2: Replace the line below with print(f"Correct: {is_correct}")
print(f"The correct answer is: {correct}")
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding