Score Thresholds
Use elif to show a grade message based on the player's final score
Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.
The quiz shows Score: 2/3, but it doesn't tell the player how they did. You need a message that reacts to the score. With three questions, there are four meaningful outcomes: 0, 1, 2, or 3 correct.
The elif clause
When you have more than two outcomes, use elif (short for "else if") to check additional conditions:
if condition_1:
# runs when condition_1 is True
elif condition_2:
# runs when condition_1 is False AND condition_2 is True
else:
# runs when none of the above matchedPython checks conditions from top to bottom. It runs the first block that matches, then skips the rest.
Applying it to PyQuiz
After printing the score, add a grade message with four levels:
if score == total:
print("Perfect score!")
elif score >= 2:
print("Great job!")
elif score >= 1:
print("Not bad!")
else:
print("Keep practicing!")A score of 3 matches score == total. A score of 2 skips the first check and matches score >= 2. A score of 1 skips the first two and matches score >= 1. A score of 0 falls through to else.
Why order matters
If you put score >= 1 before score >= 2, a score of 2 would match score >= 1 first and print "Not bad!" — even though "Great job!" is the right message. Always put the most restrictive condition first.
Instructions
Add a grade message after the score display.
- After
print(f"Score: {score}/{total}"), write anifstatement: ifscore == total, callprint("Perfect score!"). - Add an
elifthat checksscore >= 2. Inside, callprint("Great job!"). - Add an
elifthat checksscore >= 1. Inside, callprint("Not bad!"). - Add an
elseclause. Inside, callprint("Keep practicing!").
QUESTIONS = [
("What is the type of the value 3.14?", "int", "str", "float", "bool", 3),
("What keyword defines a function in Python?", "for", "def", "if", "class", 2),
("What does print() do?", "Stores a value", "Returns a number", "Displays output", "Reads input", 3),
]
score = 0
total = 3
print("=== Python Quiz ===")
print()
for text, o1, o2, o3, o4, correct in QUESTIONS:
print(text)
print()
print(f" 1) {o1}")
print(f" 2) {o2}")
print(f" 3) {o3}")
print(f" 4) {o4}")
print()
answer_text = input("Your answer (1-4): ")
answer = int(answer_text)
is_correct = answer == correct
print()
print(f"You answered: {answer}")
if is_correct:
score = score + 1
print("Correct!")
else:
print(f"Wrong! The correct answer was option {correct}.")
print(f"Score: {score}/{total}")
# Step 1: If score == total, print "Perfect score!"
# Step 2: elif score >= 2, print "Great job!"
# Step 3: elif score >= 1, print "Not bad!"
# Step 4: else print "Keep practicing!"
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding