The lights flash. The audience cheers. You are about to host a quiz show with scores, streak bonuses, secret follow-ups, and a SUDDEN DEATH round. Can your code keep the whole game fair?
Each idea comes as a short ladder — the simplest version, then one small step up, then the real game use — with a Notice naming the one new thing each time.
-
Keep a running score with the accumulator pattern.
-
Combine yes/no facts with
and,orandnot. -
Nest one
ifinside another for a secret follow-up question. -
End a loop early with
break.
-
Lesson 1: Score and Streaks
A quiz needs a score that can grow. An accumulator is a variable that collects a total:
score = score + 1takes the old value, adds one, and stores it back.score = 0 score = score + 1 print(f"Score: {score}")Scroll the code for more ↓
Output
Score: 1score = score + 1turns the old0into1and saves it back inscore.score = 0 score = score + 1 score = score + 1 print(f"Score: {score}")Scroll the code for more ↓
Output
Score: 2Each update builds on the last, so the total climbs to
2.score = 0 r = 0 while r < 3: score = score + 1 r = r + 1 print(f"Score after the round: {score}")Scroll the code for more ↓
Output
Score after the round: 3Inside a
whileloop the accumulator grows once per trip — three trips, three points.score = 0 asked = 0 r = 0 while r < 3: asked = asked + 1 score = score + 1 r = r + 1 print(f"Asked {asked}, scored {score}")Scroll the code for more ↓
Output
Asked 3, scored 3Two accumulators can run side by side — points earned versus questions asked — each answering its own question.
A quiz also needs rules that combine yes/no facts. Logical operators join booleans:
and,or,not.print(True and True) print(True and False)Scroll the code for more ↓
Output
True Falseandis True only when BOTH sides are True.print(True or False) print(False or False)Scroll the code for more ↓
Output
True Falseoris True when AT LEAST ONE side is True.print(not True) print(not False)Scroll the code for more ↓
Output
False Truenotflips True and False.correct = True alive = True print(correct and alive)Scroll the code for more ↓
Output
TrueCombine them for a real rule — the streak bonus needs the answer correct and the streak alive.
Here is the opening round put together. With fixed stand-in answers it runs top to bottom; the real version below reads what the player actually types.
score = 0 asked = 0 alive = True answer_1 = 8 # Fixed stand-in for the typed answer. asked = asked + 1 if answer_1 == 8: score = score + 1 else: alive = False answer_2 = "Pacific" # Fixed stand-in for the typed answer. asked = asked + 1 if answer_2 == "Pacific" or answer_2 == "pacific": score = score + 1 else: alive = False if answer_1 == 8 and alive: score = score + 1 print("Streak bonus!") print(f"Score {score} after {asked} questions.")Scroll the code for more ↓
Output
Streak bonus! Score 3 after 2 questions.With fixed stand-in answers the opening round runs top to bottom — accumulators keep the tallies,
oraccepts either spelling, andandguards the bonus (score3).Try it score = 0 asked = 0 alive = True answer_1 = int(input("What is 5 + 3? ")) asked = asked + 1 if answer_1 == 8: score = score + 1 else: alive = False answer_2 = input("Which ocean is west of California? Type Pacific: ") asked = asked + 1 if answer_2 == "Pacific" or answer_2 == "pacific": score = score + 1 else: alive = False if answer_1 == 8 and alive: score = score + 1 print("Streak bonus!") print(f"Score {score} after {asked} questions.")Scroll the code for more ↓
The real version reads each answer with
input()— same rules, live answers. (It reads live input, so it does not run here.)Lesson 2: Secret Follow-ups
A condition can live inside another condition. This is conditional nesting: the inner
ifis reached only when the outerifis True.unlocked = True correct = True if unlocked: if correct: print("Bonus won!")Scroll the code for more ↓
Output
Bonus won!The inner
ifruns only because the outerif(unlocked) was True.unlocked = True correct = False if unlocked: if correct: print("Bonus won!") else: print("Unlocked, but the answer was wrong.")Scroll the code for more ↓
Output
Unlocked, but the answer was wrong.The inner
if/elselives entirely inside the outer branch.score = 0 unlocked = True correct = True if unlocked: if correct: score = score + 1 print(f"Score: {score}")Scroll the code for more ↓
Output
Score: 1One new step — the inner branch now DOES something (scores a point) instead of just printing.
Now put the whole follow-up together: part one must be right to unlock a scored part two.
part_one = 12 final_score = 0 if part_one == 12: final_score = final_score + 1 part_two = "Saturn" if part_two == "Saturn": final_score = final_score + 2 print("Follow-up bonus!") else: print("Follow-up missed.") else: print("The follow-up stays locked.") print(f"Final score: {final_score}")Scroll the code for more ↓
Output
Follow-up bonus! Final score: 3The follow-up stays locked unless part one is correct — a gate inside a gate.
Try it part_one = int(input("Part one answer: ")) final_score = 0 if part_one == 12: final_score = final_score + 1 part_two = input("Follow-up answer: ") if part_two == "Saturn": final_score = final_score + 2 print("Follow-up bonus!") else: print("Follow-up missed.") else: print("The follow-up stays locked.") print(f"Final score: {final_score}")Scroll the code for more ↓
The real program reads the two answers with
input()— everything else is the same gate-inside-a-gate. (It reads live input, so it does not run here.)Lesson 3: SUDDEN DEATH
In the final round, one wrong answer ends everything. A
breakstatement leaves a loop immediately, jumping to the first line after it.count = 0 while count < 5: print(count) if count == 2: break count = count + 1Scroll the code for more ↓
Output
0 1 2breakstops the loop the instantcountreaches 2 — it never prints 3 or 4.r = 0 while r < 3: correct = r != 1 if not correct: print("Wrong — round over!") break print("Correct! Keep going.") r = r + 1Scroll the code for more ↓
Output
Correct! Keep going. Wrong — round over!A wrong answer triggers
break, ending the whole round early.streak = 0 r = 0 while r < 10: streak = streak + 1 if streak == 3: print("Three in a row — banking it!") break r = r + 1Scroll the code for more ↓
Output
Three in a row — banking it!One new step — here
breakfires when an accumulator (streak) reaches a target, not on a wrong answer.Now put it together as the full SUDDEN DEATH round. With fixed stand-in answers it runs top to bottom; the real version below reads what the player types.
asked = 0 score = 0 while asked < 3: if asked == 0: answer = 3 # Fixed stand-in for the typed answer. correct = answer == 3 elif asked == 1: answer = 42 # Fixed stand-in for the typed answer. correct = answer == 42 else: answer = 7 # Fixed stand-in for the typed answer. correct = answer == 7 if not correct: print("Wrong! Sudden death ends.") break score = score + 1 asked = asked + 1 print(f"Survived {score} sudden-death questions.")Scroll the code for more ↓
Output
Survived 3 sudden-death questions.With fixed correct answers the round runs to the end — a
whileloop, anif/elif/elsedispatch, andbreakready the instant an answer is wrong (survived3).Try it asked = 0 score = 0 while asked < 3: if asked == 0: answer = int(input("SUDDEN DEATH 1 — sides on a triangle? ")) correct = answer == 3 elif asked == 1: answer = int(input("SUDDEN DEATH 2 — 6 times 7? ")) correct = answer == 42 else: answer = int(input("SUDDEN DEATH 3 — days in a week? ")) correct = answer == 7 if not correct: print("Wrong! Sudden death ends.") break score = score + 1 asked = asked + 1 print(f"Survived {score} sudden-death questions.")Scroll the code for more ↓
The real version reads each answer with
input()andbreaks the moment one is wrong. (It reads live input, so it does not run here.)Algorithm Extension
Extra loop practice — optional enrichment: these drills reuse the same loop shapes on small fixed data, so try as many as you like.
Pattern Spotlight: Running total (accumulate)
When values arrive one at a time, the program needs a named box holding the total-so-far, updated each step.
A counter says how many rounds; a running total says how many points altogether.
total = 0 total = total + 5 print(total)Scroll the code for more ↓
Output
5Start the running total at
0, then add the first score — the box now holds5.total = 0 total = total + 5 total = total + 3 print(total)Scroll the code for more ↓
Output
8Each new score builds on the total-so-far, so it climbs to
8.r = 1 total = 0 while r <= 3: if r == 1: score = 5 elif r == 2: score = 3 else: score = 8 total = total + score r = r + 1 print(total)Scroll the code for more ↓
Output
16The loop feeds one score per round into the SAME running total — 5, then 8, then 16.
Try it rounds = int(input("How many rounds? ")) total = 0 r = 1 while r <= rounds: score = int(input(f"Score for round {r}: ")) total = total + score r = r + 1 print(total)Scroll the code for more ↓
The same running total, now fed by however many scores the player types — swap the fixed rounds for
input()and it works for any number of rounds. (Reads live input, so it does not run here.)Pattern Spotlight: Counting by condition (count)
Keep a counter; check each item once, bump it when it matches.
A running total adds each value; counting by condition adds exactly 1 only when the check says yes.
count = 0 score = 7 if score >= 5: count = count + 1 print(count)Scroll the code for more ↓
Output
1The counter moves only when the check passes —
7 >= 5is True, socountbecomes1.count = 0 score = 7 if score >= 5: count = count + 1 score = 3 if score >= 5: count = count + 1 print(count)Scroll the code for more ↓
Output
1Check each score once;
7passes but3fails, so only one is counted.r = 1 count = 0 while r <= 3: if r == 1: score = 7 elif r == 2: score = 3 else: score = 8 if score >= 5: count = count + 1 r = r + 1 print(count)Scroll the code for more ↓
Output
2One pass over the rounds, adding
1only when a score clears 5 — 7 and 8 pass, 3 does not, so2.Try it rounds = int(input("How many rounds? ")) r = 1 count = 0 while r <= rounds: score = int(input(f"Score for round {r}: ")) if score >= 5: count = count + 1 r = r + 1 print(count)Scroll the code for more ↓
The counter still bumps only when a score clears 5, but now it reads each score with
input()and works for any number of rounds. (Reads live input, so it does not run here.)-
score = score + 1takes the old total, adds one and saves it back. -
andneeds both sidesTrue,orneeds at least one, andnotflips a boolean. -
A nested
ifis reached only when the outerifisTrue. -
breakleaves a loop at once and jumps to the first line after it.
You can now host a quiz show with a score, a streak bonus and a SUDDEN DEATH round.
-