Python
Python

 

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

    1. Ensure you have Python installed on your computer. If not, download and install it from python.org.
    2. Copy the Python code provided above into a text file and save it with a .py extension (for example, guessing_game.py).
    3. 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
  1. Follow the on-screen instructions to play the game!
© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)