Skip to content

Beginner project · Small

Number guessing game

Your program picks a secret whole number between 1 and 100, then reads guesses and answers each one with too high, too low, or correct. When the guess is right it reports how many guesses it took. It is small enough to finish in one sitting and it still makes you combine a loop, a condition, input handling and a counter.

Languages
Python
Size
Small: one sitting for most learners
Where to build it
Runs as-is in the Python playground: type your guesses into the input box, one per line, before pressing Run. Open the Python playground →

What you will practise

  • while loops with a condition that can end early
  • comparing numbers with if, elif and else
  • reading input and converting text to a number
  • keeping a running count in a variable
  • rejecting bad input without crashing

Requirements

The project is done when every one of these is true.

  • The secret number is a whole number from 1 to 100 inclusive, and there is one clearly marked place in the code where you can fix it to a known value for testing.
  • The program reads one guess per line and prints exactly one response per guess: too high, too low, or correct.
  • When the guess is correct the program prints the number of guesses used and stops reading input.
  • A guess that is not a whole number (for example "seven" or "12.5") prints a message asking for a whole number and does not count towards the guess total.
  • A whole number outside 1 to 100 prints a message saying it is out of range and does not count towards the guess total.
  • The guess counter is correct: guessing right on the first line reports 1 guess, not 0 and not 2.
  • No line of output mentions the secret number before it has been guessed.

Milestones

A sensible order to build it in, so something works at every step.

  1. Print a single response to a single guess

    Hard-code the secret as 42, read one line, convert it to an int, and print too high, too low or correct. No loop yet, so you can see the comparison working on its own.

  2. Wrap it in a loop that ends on a correct guess

    Put the read-and-compare step inside a while loop that keeps going until the guess matches, then print a closing message after the loop.

  3. Count the guesses

    Add a counter that starts at 0 and increases by one for every guess you actually judged, then report it in the closing message.

  4. Handle input that is not a whole number

    Wrap the conversion in try and except so bad text prints a friendly message and the loop carries on instead of stopping with a traceback.

  5. Reject out-of-range numbers

    Before comparing, check the guess is between 1 and 100, and use continue so an out-of-range guess skips both the comparison and the counter.

  6. Switch the secret to a random number

    Replace the hard-coded 42 with a random whole number in the range, keeping the hard-coded value in a comment so you can put it back whenever you want to retest.

Hints

Open one only when you are stuck. Each gives a little more away.

Show hint 1

Two separate ideas are fighting for the same variable: the text the player typed and the number it represents. Keep them in two variables with different names and the conversion step becomes obvious.

Show hint 2

The counter belongs next to the comparison, not next to the input read. If you increase it as soon as you read a line, rejected input will inflate the total.

Show hint 3

continue is the tool for "this line was not a real guess": it jumps straight back to the top of the loop, skipping everything below it.

Show hint 4

In the playground, input() reads the next line from the input box. When the lines run out there is nothing left to read, so input() raises an error rather than waiting for you. Put in a few more lines than you expect to need, or catch that error and end the game politely.

Show hint 5

Halving the remaining range each time is why a good player never needs many guesses: seven halvings cover any range up to 128 numbers, which is more than 100.

How to test it

Run these checks yourself, or turn them into automated tests once you know how.

  • Fix the secret to 42 and feed the lines 50, 25, 37, 43, 42. Expect: too high, too low, too low, too high, correct in 5 guesses.
  • Fix the secret to 7 and feed a single line, 7. Expect a correct message reporting 1 guess.
  • Feed the lines seven, 7 with the secret fixed to 7. Expect a message asking for a whole number, then a correct message reporting 1 guess, not 2.
  • Feed the lines 0, 101, 50 with the secret fixed to 50. Expect two out-of-range messages and then a correct message reporting 1 guess.
  • Feed the line 12.5. Expect the same please-use-a-whole-number message as for letters, because int() rejects both.
  • Feed the lines 1 then 100 with the secret fixed to 50. Expect too low then too high, proving the comparison is not reversed.
  • Read your own output back and confirm the secret number never appears in it until the correct guess.

Stretch goals

  • Add a guess limit and a losing message that reveals the answer when the limit is reached.
  • Widen the range to 1 to 1000 and print how many guesses a perfect halving strategy would have needed.
  • Track the narrowest range still possible after each guess and print it, so the player can see the space shrinking.
  • Let the player set the upper bound on the first line of input, defaulting to 100 if that line is blank.

All projects · Roadmaps · Practice problems