Introduction
In this game, the computer randomly selects a number within a given range, and your task is to guess that number.
You will enter a guess, and the computer will tell you whether your guess is too high, too low, or correct.
The game continues until you guess the correct number. This simple game is an excellent way to practice Python programming
and understand concepts such as random number generation, loops, and conditionals.
Objective
The objective of this game is to help the user practice logical thinking and interact with the Python code.
By playing this game, you will learn how to implement random number generation, handle user input, and control
the flow of the game with conditionals and loops.
Python Code for the Number Guessing Game
# Number Guessing Game in Python import random # Display the game title print("Welcome to the Number Guessing Game!") print("I am thinking of a number between 1 and 100. Can you guess what it is?") # Generate a random number between 1 and 100 secret_number = random.randint(1, 100) # Initialize the number of attempts attempts = 0 # Start the game loop while True: # Ask the user for their guess guess = int(input("Enter your guess: ")) attempts += 1 # Check if the guess is correct, too high, or too low if guess < secret_number: print("Too low! Try again.") elif guess > secret_number: print("Too high! Try again.") else: print(f"Congratulations! You guessed the number {secret_number} in {attempts} attempts.") break # End the game when the correct number is guessed # End of the game print("Thanks for playing the Number Guessing Game!")
Explanation of the Program Structure
The Python Number Guessing Game is designed to be simple and easy to understand, while also utilizing core programming concepts.
1. Importing the random module
The game starts by importing the random
module, which allows us to generate random numbers. The computer will use this
module to select a secret number between 1 and 100.
2. Generating a random number
The random.randint(1, 100)
function generates a random integer between 1 and 100. This is the secret number
that the user has to guess.
3. Taking user input
The game then enters a loop where it asks the user to input a guess. The input()
function captures the user’s guess
as a string, and the int()
function converts it into an integer.
4. Checking the guess
After the user enters their guess, the program compares the guess with the secret number:
– If the guess is too low, the game will display “Too low! Try again.”
– If the guess is too high, it will display “Too high! Try again.”
– If the guess is correct, the game will congratulate the user and show the number of attempts made.
5. Looping and ending the game
The game continues to loop until the user guesses the correct number. When the correct guess is made, the program exits
the loop using the break
statement and thanks the user for playing.
How to Run the Program
-
- Ensure you have Python installed on your computer. If not, download and install it from python.org.
- Copy the Python code provided above into a text file and save it with a .py extension (for example,
guessing_game.py
). - Open a terminal or command prompt, navigate to the directory where your Python file is saved, and run the program by typing:
python guessing_game.py
- Follow the on-screen instructions to play the game!