Score Thresholds
Exit

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 matched

Python 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.

  1. After print(f"Score: {score}/{total}"), write an if statement: if score == total, call print("Perfect score!").
  2. Add an elif that checks score >= 2. Inside, call print("Great job!").
  3. Add an elif that checks score >= 1. Inside, call print("Not bad!").
  4. Add an else clause. Inside, call print("Keep practicing!").