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.
- After
score = 0, create a variable namedstreakand assign it0— streak starts at zero before each round and counts consecutive correct answers. - Inside
if is_correct:, afterscore = score + 1, addstreak = streak + 1— this increments the streak each time the player answers correctly. - After
print("Correct!"), add a nestedifstatement: ifstreak >= 3, callprint(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. - At the top of the
elseblock, before the wrong-answer print, addstreak = 0— a wrong answer breaks the streak, so it resets to zero before the next question.
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
# Step 1: Create streak variable set to 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
# Step 2: Add 1 to streak
print("Correct!")
# Step 3: If streak >= 3, print " Hot streak! ({streak} in a row)"
else:
# Step 4: Reset streak to 0
print(f"Wrong! The correct answer was option {correct}.")
print(f"Score: {score}/{total}")
if score == total:
print("Perfect score!")
elif score >= 2:
print("Great job!")
elif score >= 1:
print("Not bad!")
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