Lesson 63 +30 XP

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

  1. Add guess = 0 and attempts = 0.
  2. Loop with while guess != secret:.
  3. Inside, ask for a guess with input() and convert to int.
  4. Compare: higher, lower, or exact.
  5. Count each attempt.
  6. 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.