π² Build a Number Guessing Game
Classic higher/lower game with attempts counter. Practices: conditionals, loops, I/O, debugging mindset.
What You'll Build
The computer picks a secret number between 1 and 100. The player guesses; you tell them "higher" or "lower" until they get it.
Step 1 β Pick a Number
javascript
const secret = Math.floor(Math.random() * 100) + 1;Step 2 β the Guess Loop (Node.Js)
javascript
const readline = require("readline").createInterface({ input: process.stdin, output: process.stdout });
let attempts = 0;
function ask() {
readline.question("Your guess: ", (input) => {
attempts++;
const guess = Number(input);
if (guess === secret) {
console.log(`Got it in ${attempts} attempts!`);
readline.close();
} else if (guess < secret) {
console.log("Higher");
ask();
} else {
console.log("Lower");
ask();
}
});
}
ask();Step 3 β Python Version
python
import random
secret = random.randint(1, 100)
attempts = 0
while True:
guess = int(input("Your guess: "))
attempts += 1
if guess == secret:
print(f"Got it in {attempts} attempts!")
break
print("Higher" if guess < secret else "Lower")Stretch Goals
- Limit to 7 guesses (binary search territory).
- Reject non-numeric input.
- Make the bot version: write a function that always wins in β€7.