Python by Projects

unit

Quiz Show

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.

You will learn

  • Keep a running score with the accumulator pattern.
  • Combine yes/no facts with and, or and not.
  • Nest one if inside 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 + 1 takes the old value, adds one, and stores it back.

score = 0
score = score + 1
print(f"Score: {score}")

Output

Score: 1

Notice

score = score + 1 turns the old 0 into 1 and saves it back in score.

score = 0
score = score + 1
score = score + 1
print(f"Score: {score}")

Output

Score: 2

Notice

Each 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}")

Output

Score after the round: 3

Notice

Inside a while loop 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}")

Output

Asked 3, scored 3

Notice

Two 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)

Output

True
False

Notice

and is True only when BOTH sides are True.

print(True or False)
print(False or False)

Output

True
False

Notice

or is True when AT LEAST ONE side is True.

print(not True)
print(not False)

Output

False
True

Notice

not flips True and False.

correct = True
alive = True
print(correct and alive)

Output

True

Notice

Combine 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.")

Output

Streak bonus!
Score 3 after 2 questions.

Notice

With fixed stand-in answers the opening round runs top to bottom — accumulators keep the tallies, or accepts either spelling, and and guards the bonus (score 3).

Try it yourself

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.")

Notice

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 if is reached only when the outer if is True.

unlocked = True
correct = True
if unlocked:
    if correct:
        print("Bonus won!")

Output

Bonus won!

Notice

The inner if runs only because the outer if (unlocked) was True.

unlocked = True
correct = False
if unlocked:
    if correct:
        print("Bonus won!")
    else:
        print("Unlocked, but the answer was wrong.")

Output

Unlocked, but the answer was wrong.

Notice

The inner if/else lives entirely inside the outer branch.

score = 0
unlocked = True
correct = True
if unlocked:
    if correct:
        score = score + 1
print(f"Score: {score}")

Output

Score: 1

Notice

One 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}")

Output

Follow-up bonus!
Final score: 3

Notice

The follow-up stays locked unless part one is correct — a gate inside a gate.

Try it yourself

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}")

Notice

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 break statement leaves a loop immediately, jumping to the first line after it.

count = 0
while count < 5:
    print(count)
    if count == 2:
        break
    count = count + 1

Output

0
1
2

Notice

break stops the loop the instant count reaches 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 + 1

Output

Correct! Keep going.
Wrong — round over!

Notice

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 + 1

Output

Three in a row — banking it!

Notice

One new step — here break fires 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.")

Output

Survived 3 sudden-death questions.

Notice

With fixed correct answers the round runs to the end — a while loop, an if/elif/else dispatch, and break ready the instant an answer is wrong (survived 3).

Try it yourself

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.")

Notice

The real version reads each answer with input() and breaks 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)

Output

5

Notice

Start the running total at 0, then add the first score — the box now holds 5.

total = 0
total = total + 5
total = total + 3
print(total)

Output

8

Notice

Each 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)

Output

16

Notice

The loop feeds one score per round into the SAME running total — 5, then 8, then 16.

Try it yourself

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)

Notice

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)

Output

1

Notice

The counter moves only when the check passes — 7 >= 5 is True, so count becomes 1.

count = 0
score = 7
if score >= 5:
    count = count + 1
score = 3
if score >= 5:
    count = count + 1
print(count)

Output

1

Notice

Check each score once; 7 passes but 3 fails, 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)

Output

2

Notice

One pass over the rounds, adding 1 only when a score clears 5 — 7 and 8 pass, 3 does not, so 2.

Try it yourself

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)

Notice

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.)

Recap

  • score = score + 1 takes the old total, adds one and saves it back.
  • and needs both sides True, or needs at least one, and not flips a boolean.
  • A nested if is reached only when the outer if is True.
  • break leaves 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.

Go to the exercises (21)

For parents and teachers: report a problem