Chapter 11: Your First Project - The Number Guessing Game
Welcome to Chapter 11! You have learned about variables, data types, loops, conditions, and functions. Now it's time to combine all those skills to create something fun and interactive: a classic number guessing game.
Building projects is the best way to solidify your knowledge. You'll see how all the individual concepts you've learned work together to create a complete, working program.
The Goal
The objective is simple:
The computer will think of a secret random number between 1 and 100.
The player will repeatedly guess the number.
After each guess, the computer will tell the player if their guess was too high or too low.
The game ends when the player correctly guesses the secret number.
The Game Plan: Thinking Like a Programmer
Before writing any code, it's a good habit to break the problem down into smaller, manageable steps.
Generate a secret number: We need a way for the computer to pick a random number. How do we do that?
Start a loop: The game needs to continue until the player guesses correctly. A loop is the perfect tool for this.
Get the player's guess: We need to ask the player for their input.
Convert the input: The
input()function gives us a string, but we need to compare it to a number. We must convert it to an integer.Compare and give feedback: We need to use
if,elif, andelseto check if the guess is greater than, less than, or equal to the secret number.End the game: If the guess is correct, we'll print a winning message and stop the loop.
A New Tool: The random Module
random ModuleTo get a random number, we need to use a module. A module in Python is a file containing code written by someone else that we can use in our program. Python has a rich standard library full of useful modules.
The module we need is called random. To use it, we must first import it at the top of our file.
Once imported, we can use the functions from that module. The one we want is random.randint(a, b), which gives us a random integer between a and b (inclusive).
Let's Build the Game!
Here is the complete code. We will break it down piece by piece right after.
Code Breakdown
import random: We make therandommodule available to our program.def number_guessing_game():: We've wrapped our entire game inside a function. This is great practice for keeping our code organized.secret_number = random.randint(1, 100): We call therandintfunction to generate our secret number.while True:: This creates an infinite loop. The game will keep asking for guesses forever until we explicitly tell it to stop.guess_str = input(...): We get the player's input as a string.if not guess_str.isdigit():: This is a handy safety check. The string method.isdigit()returnsTrueonly if all characters in the string are digits. If not, we print an error andcontinuesends the execution right back to the start of the loop, asking for input again.guess = int(guess_str): If the input was valid, we convert it to an integer.if/elif/elseblock: This is the core logic of our game. We compare the guess to the secret number and print the appropriate feedback.break: This is the crucial line! When the player guesses correctly (theelseblock),breakimmediately terminates thewhileloop, and the program continues after the loop (in this case, the function and program end).number_guessing_game(): At the very end, we call the function to start the game.
Summary and What's Next
Congratulations! You have successfully built your first complete, interactive project. You imported a module, took user input, managed the flow of the game with a loop and conditional logic, and put it all inside a well-organized function.
Feel free to expand on this game! Here are some ideas for a challenge:
Limit the player to a certain number of guesses (e.g., 10 tries).
After the game is over, ask the player if they want to play again.
Let the player choose the range for the secret number (e.g., 1-50 or 1-1000).
You now have a solid foundation in the core principles of programming. In the next chapters, we will begin to explore more advanced topics, starting with how to read from and write to files on your computer.
Last updated