Score and Streak
Exit

Score and Streak

Track a running streak and show a hot streak message after three correct answers in a row

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

The quiz tracks total score, but it doesn't reward consistency. A player who answers all three correctly in a row should get some recognition. PyQuiz uses a streak — a count of consecutive correct answers — to do this.

The streak variable

Add streak = 0 next to score = 0. The streak goes up by one on a correct answer and resets to zero on a wrong answer:

if is_correct:
    score = score + 1
    streak = streak + 1
    print("Correct!")
else:
    streak = 0
    print(f"Wrong! ...")

Nested if for the bonus message

Inside the if is_correct: block, add a nested if to check whether the streak is high enough to show a bonus message:

if is_correct:
    score = score + 1
    streak = streak + 1
    print("Correct!")
    if streak >= 3:
        print(f"  Hot streak! ({streak} in a row)")

The nested if only runs when is_correct is already True. With three questions, the hot streak message fires when the player answers all three correctly.

Instructions

Add streak tracking to quiz.py.

  1. After score = 0, create a variable named streak and assign it 0 — streak starts at zero before each round and counts consecutive correct answers.
  2. Inside if is_correct:, after score = score + 1, add streak = streak + 1 — this increments the streak each time the player answers correctly.
  3. After print("Correct!"), add a nested if statement: if streak >= 3, call print(f" Hot streak! ({streak} in a row)") — this fires only when the player has been correct three or more times in a row without a miss.
  4. At the top of the else block, before the wrong-answer print, add streak = 0 — a wrong answer breaks the streak, so it resets to zero before the next question.