Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

A number-guessing game is a fun beginner coding project because it uses simple rules to create a real interactive game. The computer secretly chooses a random number, and the player tries to guess it. After each guess, the program gives feedback such as too high, too low, or correct.

This project matters because it teaches the core ideas behind many games and apps: input, decisions, loops, and variables.

In Scratch or Python, the game works by storing a secret number and comparing it with the player's guess. A loop keeps the game running until the guess matches the secret number. A counter keeps track of how many guesses the player has used.

Once you understand this pattern, you can add levels, scoreboards, hints, timers, and other creative features.

Understanding Code a Number-Guessing Game

The important idea is that each message reduces the set of possible answers. If a guess is too low, every number below that guess can be ignored. If it is too high, every number above it can be ignored.

Students can use this to play more efficiently. Start near the middle of the range. Then keep choosing the middle of the remaining range.

This method is called a binary search strategy. In a range from one to one hundred, it can find the answer in about seven well chosen guesses. The game therefore connects coding with logical problem solving and number sense.

The order of instructions matters a great deal. A program must get a new guess before it can compare that guess with the secret number. It must decide which feedback to show before it asks again.

The counter should increase once for every real attempt. If the counter is placed in the wrong part of a repeating section, it may increase too many times or not at all. This is a common bug.

Trace the program by hand with a small example, such as a secret of twelve and guesses of five, fifteen, then twelve. Write down the value held in each variable after every step. This makes invisible program actions easier to understand.

Real players do not always follow instructions perfectly. They may type a word, leave an answer blank, or enter a number outside the chosen range. Python input starts as text, so the program needs to convert valid text into a whole number before comparing it with the secret number.

Scratch handles number input more simply, but a project can still check whether the answer is within the allowed limits. A clear response for invalid input prevents confusing results. Good programs treat mistakes as normal events, not as failures by the player.

Randomness needs careful testing because one game round cannot show every possible result. Test secrets near both ends of the range, such as one and one hundred. Test a correct first guess, several wrong guesses, repeated guesses, and invalid entries if the program accepts typed text.

Check that the winning message appears only when it should and that the final count is accurate. This same habit is used in larger software, from quiz apps to online forms. Small projects teach an important lesson.

Code is not finished when it runs once. It is finished when it behaves reliably in many different situations.

Key Facts

  • A random number can be chosen with random.randint(1, 100) in Python.
  • The player input must be stored in a variable, such as guess.
  • A comparison checks the guess: guess > secret, guess < secret, or guess == secret.
  • A loop repeats the guessing steps until the player is correct.
  • The guess counter increases by 1 each time the player makes a guess: guesses = guesses + 1.
  • Good game feedback tells the player what to try next, such as too high or too low.

Vocabulary

Variable
A variable is a named place in a program that stores information, such as the secret number or the player's guess.
Random number
A random number is a number chosen by the computer in a way that is unpredictable to the player.
Loop
A loop is a part of a program that repeats instructions until a condition tells it to stop.
Conditional
A conditional is an if statement that makes a program choose what to do based on whether something is true or false.
Input
Input is information the user gives to the program, such as typing a guess into a box.

Common Mistakes to Avoid

  • Forgetting to change the player's input into a number is wrong because typed input may be treated as text, which cannot be compared correctly with the secret number.
  • Putting the random number inside the loop is wrong because the secret number will keep changing after every guess.
  • Forgetting to increase the guess counter is wrong because the program will not know how many tries the player used.
  • Using only one if statement for all feedback is wrong if it does not check too high, too low, and correct separately, because the player may get missing or confusing feedback.

Practice Questions

  1. 1 A secret number is 62. The player guesses 40, 75, 60, and 62. Write the feedback after each guess and state the final number of guesses.
  2. 2 In a game from 1 to 100, a player always cuts the possible range in half. Starting with 50, then using the feedback, what guess should the player make next if the feedback is too low?
  3. 3 Explain why the secret number should be chosen before the guessing loop starts instead of inside the loop.