Skip to Content
Course content

62: Practice Exercise: Building a Number Guessing Game

Click on the "Edit" button in the top corner of the screen to edit your slide content.

A few years ago, I was mentoring a junior developer who wanted to build a simple game for his portfolio. He spent three days wrestling with a complex GUI library, trying to make a fancy window with buttons and animations, before he had even written a single line of the actual game logic. By the time he got the window to pop up, he realized he hadn't actually figured out how to handle the "win condition" or the random number generation. He was exhausted and frustrated. I told him the same thing I'll tell you: always build your engine in the console first. If the logic doesn't work in a plain text terminal, a fancy interface won't save it.

That's why we're building a Number Guessing Game. It seems simple, but it's actually a perfect litmus test for your grasp of the "big three" in early Python: loops, conditionals, and type casting. You're not just printing text; you're managing a state—the secret number—and reacting to user input until a specific goal is met.

Managing the Game Loop and Randomness

To start, you'll need the random module. Specifically, random.randint(a, b) is your best friend here because it includes both endpoints, which is exactly what players expect when you tell them the number is between 1 and 100. The real meat of the program, however, is the while loop. You don't know if the user will guess the number on the first try or the fiftieth, so a for loop is useless here. You need a loop that runs indefinitely—or until a boolean flag changes—to keep the game alive.

import random

secret_number = random.randint(1, 100)
guess = None

while guess != secret_number:
    # This is where the interaction happens
    guess = int(input("Take a guess: "))

Notice that I initialized guess as None. If you don't, Python will throw a NameError the moment the while loop tries to check if guess equals the secret_number. It's a small detail, but these are the things that usually trip people up in their first few practice projects.

Refining the Feedback Logic

A game that just says "Wrong" is boring. To make it a real game, you need to provide directional feedback. This is where your if/elif/else chain comes in. You want to check if the guess is too high, too low, or exactly right. I usually recommend using elif for the second condition. Since the guess can't be both "too high" and "too low" simultaneously, using elif saves the computer from performing an unnecessary check once the first condition is met.

I also like to add an attempt counter. It adds a layer of tension. By initializing a variable like attempts = 0 and incrementing it by one inside the loop, you can tell the user exactly how many tries it took them. It turns a simple script into a challenge. Just remember that input() always returns a string, so if you forget to wrap it in int(), your comparison operators will fail or throw a TypeError. I've seen seasoned devs make that mistake when switching between languages; it happens to the best of us.




📋 Practical Task

Exercise: The High-Low Challenge with Attempt Limits

Build a complete Number Guessing Game that implements the following requirements:

  • The program should randomly select a number between 1 and 100.
  • The user should have a maximum of 7 attempts to guess the number.
  • After each guess, the program must tell the user if their guess was "Too high", "Too low", or "Correct!".
  • If the user guesses correctly, the program should congratulate them and display the total number of attempts used.
  • If the user runs out of attempts without guessing the number, the program should reveal the secret number and tell them they've lost.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.