Course outline · 0% complete

0/32 lessons0%

Course overview →

Project: Number Guessing Game

lesson 10-1 · ~12 min · 30/32

Recall from lesson 5-2 that inside a while True: loop, the only way out is a break.

The condition True never becomes false on its own, so the loop has no natural end. A break somewhere in the body, normally guarded by an if, is what supplies one.

That loop-until-done shape is the skeleton of the guessing game built in this lesson. The program cannot know in advance how many guesses the player will need, which is exactly the situation while True with a break exists to handle.

The game

The program knows a secret number. The player guesses. After each guess the program answers higher, lower, or correct!, and the game ends on a correct guess.

The plan, in pieces you already own:

  1. Store the secret (we fix it at 47 so tests are repeatable).
  2. while True: to keep the game going (lesson 5-2).
  3. int(input()) to read each guess (lesson 3-2).
  4. An if/elif/else chain to compare (lesson 4-1).
  5. break on the correct guess.

The stdin panel plays the part of the player, guessing 50, 40, 45, then 47.

The core game loop

Four guesses arrive from stdin, and the comparison chain answers each one before the final match ends the loop.

secret = 47
while True:
    guess = int(input())
    if guess < secret:
        print("higher")
    elif guess > secret:
        print("lower")
    else:
        print("correct!")
        break

Input

50
40
45
47

Output

lower
higher
higher
correct!

Following the first guess through by hand shows the whole mechanism: 50 is greater than 47, so the first test fails, the elif succeeds, and the program prints lower, meaning the player should aim lower. Then 40 and 45 are both below the secret and produce higher.

The break lives in the else branch, which is the only branch reached when the guess is neither too low nor too high. That makes the exit condition exact, and it is why the loop cannot end on a wrong answer.

Adding an attempt counter

The count pattern from lesson 5-3 slots into the game with two extra lines, one before the loop and one inside it.

secret = 47
tries = 0
while True:
    guess = int(input())
    tries += 1
    if guess < secret:
        print("higher")
    elif guess > secret:
        print("lower")
    else:
        print(f"correct in {tries} tries")
        break

Input

50
40
45
47

Output

lower
higher
higher
correct in 4 tries

tries = 0 has to sit outside the loop, since resetting it on every pass would leave it at 1 forever. The increment goes immediately after the guess is read, before any comparison, which is what makes it count every guess rather than only the wrong ones.

That placement explains the reported 4. Three guesses missed and the fourth was correct, and the counter had already been bumped for the winning guess by the time the else branch printed it.

With guesses of 30, then 60, then 47 against a secret of 47, the three printed lines are higher, lower, and correct in 3 tries.

The first guess is below the secret, so the program asks for a bigger number. The second overshoots and gets lower. The third matches exactly and takes the else branch.

The count of 3 is the detail worth checking. The counter increments right after each guess is read, including the winning one, so a correct third guess reports 3 rather than 2. Answering 2 would mean the increment sat inside the wrong-guess branches instead, which is a reasonable design for a different game but not this one.