-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
62: Practice Exercise: Building a Number Guessing Game
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.
There are no comments for now.