Number Guessing Logic
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
Pick a secret number
Declare a variable
targetand assign it a random integer between 1 and 100. In pseudocode you can writetarget ← random(1, 100). The value must be hidden from the player.Codetarget ← 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.
Hint
Use a random function from your language's standard library, e.g. Math.random() in JS or random.randint() in Python.
- 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.Codeprint("Enter your guess: ") guess ← read_input()Expected result
The program waits for the player to type a number.
- 3
Loop until correct
Wrap the comparison in a
while guess ≠ targetloop. Inside the loop, print "Higher!", "Lower!", or break when equal. After each hint, read a new guess so the loop can terminate.Codewhile 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.
Hint
Make sure the input read inside the loop updates
guess, otherwise the loop runs forever. - 4
Count the attempts
Add an
attemptscounter that starts at 0 and increments each time the player guesses. Print it at the end so the player sees their score.Codeattempts ← 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
// Pseudocode scaffold — fill in the loop body
target ← random(1, 100)
guess ← 0
attempts ← 0
// TODO: loop while guess ≠ target
// - compare guess with target
// - print hint
// - read new guess
// - increment attempts
print("Solved in", attempts, "tries.")target ← random(1, 100)
guess ← read_input()
attempts ← 1
while guess ≠ target:
if guess < target:
print("Higher!")
else:
print("Lower!")
guess ← read_input()
attempts ← attempts + 1
print("You got it in", attempts, "tries.")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
- Random Number Generator
See how real random values are produced in a browser tool.
Related Cheatsheets
- JavaScript Cheatsheet
Syntax reference for loops and conditionals in JS.
Related Reference
- JavaScript Math
Math.random and Math.floor reference for random number generation.
Related Errors
- Infinite Loop
Loops that never terminate.
- Wrong Condition
Off-by-one and logic errors.
Related Glossary
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Related Errors
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
- Infinite Loop
Loops that never terminate.
- Wrong Condition
Off-by-one and logic errors.