>> Module 01 / Day 10 / Lecture

Mini-Quiz 1 + Guessing Game

Day 10 of a 40-day journey
First a quick check on loops & conditions — then we build your first interactive game: a number you have to guess.
Lecture
75 min · 09:00–10:15
Lab
60 min · Number Guessing Game
Module
M1 · Python Foundations
DAY 10 / 40
>> First 15 minutes

Mini-Quiz 1 — right now

Before any new material, a quick 15-minute check on what you've built this week. Two short problems — close your laptops to notes, open your editor.

Q1 · LOOPS

A short for / while problem — read the loop, predict (or produce) the output.

Q2 · CONDITIONS

An if / elif / else problem — pick the right branch for the right input.

>> After the quiz

Today's roadmap

  1. Mini-Quiz 1 — 15 min on loops + conditions (done first)
  2. The game loop — while True + break
  3. Tracking state — a counter variable across passes
  4. Giving feedback — if / elif / else on the guess
  5. Mini-project brief — Number Guessing Game
>> The hook

Every game is the same loop.

Ask the player to do something → check what they did → tell them how it went → repeat. That cycle is the game loop. Today you'll build the smallest possible one: the computer holds a secret number, and the player keeps guessing until they nail it.

the_idea.txt
# loop forever:
#   1. ask for a guess
#   2. is it too high? too low? correct?
#   3. tell the player
#   4. if correct -> stop. otherwise -> go again.
>> 01 / 03 · The loop that never ends (on purpose)

while True + break

while True: loops forever — the condition is always true. The only way out is break.

forever.py
while True:
    answer = input("Say 'stop': ")
    if answer == "stop":
        print("Okay, stopping.")
        break          # leave the loop
    print("Still going...")
No break = forever.
If you forget the break, the program never stops. Press Ctrl + C to force-quit a runaway loop.
>> 02 / 03 · Remembering things across passes

Tracking state with a counter

The loop forgets nothing if you keep a variable outside it. To count guesses: start at 0 before the loop, add 1 each pass.

counter.py
tries = 0            # before the loop

while True:
    guess = int(input("Guess: "))
    tries = tries + 1   # or: tries += 1
    print(f"That was guess #{tries}")
    if guess == 42:
        break
Outside, not inside.
Put tries = 0 before the loop. Inside, it would reset to 0 every pass and never count up.
>> 03 / 03 · Three answers, three branches

Feedback with if / elif / else

A guess is exactly one of three things: too high, too low, or correct. One branch each.

feedback.py
if guess > SECRET:
    print("Too high!")
elif guess < SECRET:
    print("Too low!")
else:                          # the only case left: equal
    print(f"Correct! You got it in {tries} tries.")
    break
>> Putting it together

The whole game, in one screen

Loop + counter + feedback — every idea from today, working together.

guessing_game.py
SECRET = 42   # Day 29: random will pick this for you
tries = 0

while True:
    guess = int(input("Guess the number (1-100): "))
    tries = tries + 1
    if guess > SECRET:
        print("Too high!")
    elif guess < SECRET:
        print("Too low!")
    else:
        print(f"Correct! You got it in {tries} tries.")
        break
>> Common failures

Five mistakes you'll make today

  1. Forgetting the break — the game never ends.
  2. Putting tries = 0 inside the loop, so it never counts up.
  3. Forgetting int() around input() — comparing text to a number.
  4. Using if three times instead of if / elif / else.
  5. Mixing up > and < — "Too high" prints when it's actually low.
>> 11:00 – 12:00 · Mini-project

Your lab — Number Guessing Game

THE BRIEF
play_game()

Loop asking for a guess. Print Too high! / Too low!; on a correct guess, print Correct! You got it in N tries. and stop. Count the attempts.

THE SECRET
SECRET = 42

Hard-code it at the top — random comes on Day 29. Today is about the loop, not the randomness.

>> If you finish early

★ Stretch — warmer / colder

After each guess (except the first), tell the player whether they're getting Warmer or Colder — closer to the secret than last time, or further away.

warmer_colder.py
# abs() = distance, ignoring sign:  abs(-8) -> 8
distance      = abs(guess - SECRET)
prev_distance = abs(prev_guess - SECRET)

if distance < prev_distance:
    print("Warmer")
elif distance > prev_distance:
    print("Colder")
>> End of Day 10

What you can now do

  • ✓ Build a game loop with while True and break.
  • ✓ Track state across passes with a counter variable.
  • ✓ Give the right feedback with if / elif / else.
01 / 12
←/→ navigate   F fullscreen   Ctrl/⌘+P save as PDF