Loading lessons...
Project 3: Number Guessing Game
Project 3: Number Guessing Game
The computer picks a secret number and you try to find it with hints.
The goal
Generate a random number from 1 to 100, keep asking for guesses, and print "Too high" or "Too low" until the guess is right. Count the attempts.
What you practice
random.randint()- while loops
- if / elif conditions
- Counting attempts
Starter code
import random
secret = random.randint(1, 100)
print("I picked a number from 1 to 100.")
Step-by-step
- Add
guess = 0andattempts = 0. - Loop with
while guess != secret:. - Inside, ask for a guess with input() and convert to int.
- Compare: higher, lower, or exact.
- Count each attempt.
- After the loop, print
You got it in {attempts} tries!.
Checklist
- [ ] random.randint picks the secret
- [ ] The while loop runs until the guess matches
- [ ] Too high and too low hints work
- [ ] Attempts are counted
- [ ] A wrong input doesn't crash the game
- [ ] The program ends with a win message
TL;DR
random.randint(1, 100)gives a random whole number.- A while loop repeats until the guess matches.
- Count attempts as you go.