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.

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.