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":
breakbreak 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 = 0andstreak = 0— reset before each round- The
forloop 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.
- After
total = 3, addwhile True:— this starts a loop that runs the quiz indefinitely until the player chooses to stop. - Indent everything from
score = 0through the grade message (including theforloop and everything inside it) by one level — these are the parts that should repeat each round.QUESTIONSandtotalstay outside because they don't change between rounds. - At the end of the
whileloop body, after the grade message, add a blankprint(), then create a variable namedagainand assign itinput("Play again? (y/n): ")— the blank line separates the grade from the prompt, and the input pauses to let the player decide. - After the
againassignment, add anifstatement: ifagain != "y", usebreakto exit the loop —breakstops thewhile Trueloop immediately; any answer other than"y"ends the game. - After the
whileloop (at the top level, no indentation), callprint("Thanks for playing!")— this runs once after the loop ends, giving the player a closing message.
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),
]
total = 3
# Step 1: Add while True: here
score = 0
streak = 0
# Step 2: Indent everything below this line by one level (inside the while loop)
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
streak = streak + 1
print("Correct!")
if streak >= 3:
print(f" Hot streak! ({streak} in a row)")
else:
streak = 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!")
# Step 3: Add print(), then again = input("Play again? (y/n): ")
# Step 4: If again != "y", break
# Step 5: After the while loop, print "Thanks for playing!"
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding