──────────────────────────────────────

16. Game Loops · Build Number Quest

Make a three-guess game with higher/lower feedback and an early win.

Lesson files · lessons/loops-and-games/number-quest

Before you start
- Use a while loop that checks a changing value.
- Use if, elif and else to choose one result.
- Use and to require two conditions to be true.

By the end
- Use a counter and a True or False value to track the game.
- Test a win on the first guess and a loss after three guesses.
- Explain why the game skips the loss message after a win.

Where you can use it
Add a fixed number of tries to a word puzzle. Track how many are left and finish as soon as the player solves it.

The worked example reads one guess. The equality test guess == secret gives True when the guess matches. For a full round, start with attempts_left at 3 and won at False.

Use while attempts_left > 0 and not won to keep offering guesses. The expression not won is True while won is False. Inside the loop, subtract one attempt after each guess and set won to True when the player is correct.

After the loop, use if not won to print the losing message. Trace attempts_left and won for a first-guess win and a run of three wrong guesses.

Predict
What should the game do immediately after a correct guess?
- Require every remaining guess.
- Finish the round.

Worked example (example.py)

secret = 7
print("Guess 1 to 9.")
guess = int(input())
if guess == secret:
    print("Correct!")
else:
    print("Try again.")


Example input
2

Example result
Guess 1 to 9.
Try again.

Print Guess 1 to 9. before each guess. Give at most three guesses at the secret 7. Print Correct! for a win, Too low. for a low guess and Too high. for a high one. If all guesses are wrong, print Out of tries. The number was 7.

Starter program (starter.py)

secret = 7
print("Guess 1 to 9.")
guess = int(input())
if guess == secret:
    print("Correct!")
else:
    print("Try again.")


Starter input
2

Hint
Track attempts_left and won. Read a guess and subtract one attempt inside while attempts_left > 0 and not won.

Case 1. Try the sample
Input
2
9
7
Expected output
Guess 1 to 9.
Too low.
Guess 1 to 9.
Too high.
Guess 1 to 9.
Correct!

Case 2. Win on the first turn
Input
7
Expected output
Guess 1 to 9.
Correct!

Case 3. Use all three tries
Input
2
4
8
Expected output
Guess 1 to 9.
Too low.
Guess 1 to 9.
Too low.
Guess 1 to 9.
Too high.
Out of tries. The number was 7.

Project checklist
Run the sample case.

Project checklist
Try one different case and explain what happened.

Marking criterion
Program behaviour (4 points)
Each required behaviour works on the supplied cases.

Marking criterion
Use of the lesson concept (3 points)
The code uses the main idea in a clear, purposeful way.

Marking criterion
Test evidence (2 points)
The reflection describes a case, its expected result and what happened.

Marking criterion
Explanation (1 points)
The learner explains a change or design decision in their own words.

Stretch
Make the attempt limit a named setting and try a two-turn game with a new secret.

Reflect
Which condition stops the program in each of the two endings?

Check your prediction
Finish the round. The loop condition can combine attempts left with whether the player won.
