Keep Playing

Wrap the quiz in a while loop so the player can play again without restarting the script

💻

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

The quiz runs once and exits. If the player wants another round, they have to restart the script. A play-again prompt fixes this — and it requires a while loop.

The while True pattern

while True: creates a loop that runs forever until you explicitly stop it:

while True:
    # quiz logic here
    again = input("Play again? (y/n): ")
    if again != "y":
        break

break exits the loop immediately. When the player types anything other than "y", the loop stops.

What moves inside the loop

The while True: loop wraps everything that should repeat:

  • score = 0 and streak = 0 — reset before each round
  • The for loop and all its contents
  • The score print and grade message

The QUESTIONS list and total = 3 stay outside the loop — they don't change between rounds.

After the while loop ends, add print("Thanks for playing!").

Instructions

Wrap the quiz in a play-again loop.

  1. After total = 3, add while True: — this starts a loop that runs the quiz indefinitely until the player chooses to stop.
  2. Indent everything from score = 0 through the grade message (including the for loop and everything inside it) by one level — these are the parts that should repeat each round. QUESTIONS and total stay outside because they don't change between rounds.
  3. At the end of the while loop body, after the grade message, add a blank print(), then create a variable named again and assign it input("Play again? (y/n): ") — the blank line separates the grade from the prompt, and the input pauses to let the player decide.
  4. After the again assignment, add an if statement: if again != "y", use break to exit the loop — break stops the while True loop immediately; any answer other than "y" ends the game.
  5. After the while loop (at the top level, no indentation), call print("Thanks for playing!") — this runs once after the loop ends, giving the player a closing message.