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.
A short for /
while problem — read the loop,
predict (or produce) the output.
An if / elif / else problem —
pick the right branch for the right input.
while True + breakif / elif / else on the guessAsk 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.
# 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.
while True + break
while True: loops forever — the condition is
always true. The only way out is break.
while True:
answer = input("Say 'stop': ")
if answer == "stop":
print("Okay, stopping.")
break # leave the loop
print("Still going...")
break, the program
never stops. Press Ctrl + C
to force-quit a runaway loop.
The loop forgets nothing if you keep a variable outside it.
To count guesses: start at 0
before the loop, add 1 each pass.
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
tries = 0 before the loop.
Inside, it would reset to 0 every pass and never count up.
if / elif / elseA guess is exactly one of three things: too high, too low, or correct. One branch each.
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
else works here. If it's not greater and
not less, it must be equal. No need to test == again.
Loop + counter + feedback — every idea from today, working together.
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
break — the game never ends.tries = 0 inside the loop, so it never counts up.int() around input() — comparing text to a number.if three times instead of if / elif / else.> and < — "Too high" prints when it's actually low.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.
Hard-code it at the top — random comes on
Day 29. Today is about the loop, not the randomness.
students/<your-username>/D10_guessing_game.py.
Full brief in lab/module_1_foundations/D10_guessing_game/problem.md. PR before 12:00.
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.
# 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")
while True and break.
if / elif / else.
return.