Skip to content

Number Guessing Game

Beginner~15 min

What you'll build

A Python script that picks a random number between 1 and 100, reads guesses in a loop, prints "Higher!" or "Lower!" after each wrong guess, and congratulates the player when they win.

Goals

  • Generate a random integer with random.randint
  • Read user input with input() and convert it to int
  • Use a while loop that repeats until the guess is correct
  • Track the number of attempts and print it at the end

Prerequisites

  • Variables and assignment
  • if/else statements
  • while loops

Steps

  1. 1

    Import random and pick a target

    Import the random module and pick a secret number with random.randint(1, 100). Print a welcome message so the player knows the range.

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

    Expected result

    Running the script prints the welcome message; target holds a hidden number.

  2. 2

    Read the first guess

    Use input() to read a guess and int() to convert it to a number. Reading input is what lets the loop make progress.

    Code
    guess = int(input("Enter your guess: "))

    Expected result

    The script waits for the player to type a number and stores it in guess.

  3. 3

    Loop until correct with hints

    Wrap the comparison in a while guess != target loop. Inside, print "Higher!" if guess is too low, "Lower!" if too high, then read a new guess. The loop exits when the guess matches.

    Code
    while guess != target:
        if guess < target:
            print("Higher!")
        else:
            print("Lower!")
        guess = int(input("Enter your guess: "))
    print("You got it!")

    Expected result

    Wrong guesses print a hint and ask again; a correct guess prints the win message.

  4. 4

    Count the attempts

    Add an attempts counter that starts at 1 and increments inside the loop. Print it at the end so the player sees their score.

    Code
    attempts = 1
    while guess != target:
        if guess < target:
            print("Higher!")
        else:
            print("Lower!")
        guess = int(input("Enter your guess: "))
        attempts += 1
    print(f"You got it in {attempts} tries!")

    Expected result

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

Try it yourself

What you learned

You combined random.randint, a while loop, conditional branches, and input reading into a complete interactive game — the same control-flow shape used by many CLI programs.

Related Resources

Related Lessons

  • Loops

    The while loop that drives the guessing loop.

  • Conditions

    if/else branches that compare the guess to the target.

Related Practice

Related Tools

Related Cheatsheets

Related Reference

Related Errors

Related Glossary

  • Loop

    Repeating code blocks.

  • Variable

    Variable assignment and state.

  • Python

    Python language concepts.

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.

  • Python

    Python language concepts.

Related Errors

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

← Back to topic