Skip to content

Number Guessing Logic

Beginner~15 min

What you'll build

A small pseudocode-driven number guessing routine that picks a secret number, reads guesses, and tells the player whether to go higher or lower until they win.

Goals

  • Understand how a random target is generated and kept hidden
  • Practice a while-loop that repeats until a condition is met
  • Use conditional branches (higher / lower / equal) to guide the player
  • Translate a real game rule into structured pseudocode

Prerequisites

  • Variables and assignment
  • if/else conditional statements
  • while loops (or general loop concept)

Steps

  1. 1

    Pick a secret number

    Declare a variable target and assign it a random integer between 1 and 100. In pseudocode you can write target ← random(1, 100). The value must be hidden from the player.

    Code
    target ← random(1, 100)
    print("I picked a number between 1 and 100.")

    Expected result

    The program announces it has a secret number; the player does not see the value.

  2. 2

    Read the first guess

    Prompt the player for a guess and store it in a variable guess. Reading input is the only way the loop can make progress.

    Code
    print("Enter your guess: ")
    guess ← read_input()

    Expected result

    The program waits for the player to type a number.

  3. 3

    Loop until correct

    Wrap the comparison in a while guess ≠ target loop. Inside the loop, print "Higher!", "Lower!", or break when equal. After each hint, read a new guess so the loop can terminate.

    Code
    while guess ≠ target:
        if guess < target:
            print("Higher!")
        else:
            print("Lower!")
        guess ← read_input()
    print("You got it!")

    Expected result

    Each wrong guess prints a hint and asks again; a correct guess exits the loop and prints a win message.

  4. 4

    Count the attempts

    Add an attempts counter that starts at 0 and increments each time the player guesses. Print it at the end so the player sees their score.

    Code
    attempts ← 0
    while guess ≠ target:
        attempts ← attempts + 1
        ...
    print("Solved in", attempts, "tries.")

    Expected result

    After winning, the program shows how many guesses the player used.

Try it yourself

What you learned

You practiced combining a random generator, a while-loop, and conditional branches into a complete game loop — the same shape used by many real games and interactive prompts.

Related Resources

Related Lessons

  • Loops

    The while-loop pattern this project relies on.

  • Conditions

    if/else branches used to compare the guess with the target.

Related Practice

  • Variables MCQ

    Check your understanding of variable assignment used in the loop.

  • Functions Find

    Practice reading small functions that wrap logic like this.

Related Tools

Related Cheatsheets

Related Reference

  • JavaScript Math

    Math.random and Math.floor reference for random number generation.

Related Errors

Related Glossary

  • Loop

    Repeating code blocks.

  • Variable

    Variable assignment and state.

Related Glossary

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

  • Loop

    Repeating code blocks.

  • Variable

    Variable assignment and state.

Related Errors

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

← Back to topic