Build Your Own Number Guessing Game in Python!

Guess the Number Game

Hey there! Ready to dive into creating your very own game with Python? Today, we’re going to build a simple yet fun guessing game where your task is to guess a randomly generated number between 1 and 100. This little project is perfect for beginners and a great way to get comfortable with Python’s basic programming structures like loops, conditionals, and of course, input and output operations. Let’s break it down step by step, and don’t worry—I’ll guide you through each part!

 

What You’ll Learn

By building this game, you’ll understand how to:

  • Generate random numbers

  • Use loops to handle repeated actions

  • Apply conditionals to check the player’s guesses

  • Interact with users through inputs and outputs

 

Step 1: Setting Up Your Game

First things first, we need to set up our Python environment. If you haven’t already, make sure Python is installed on your computer. Now, open up your favorite text editor or an IDE (like PyCharm or Visual Studio Code) and create a new Python file named guess_the_number.py.

You can also run the number guessing game program online using Replit. Replit is a powerful online development environment that lets you write, run, and share code right from your browser. It's a great tool for beginners to easily test and execute their Python scripts without installing any software. Just create a new Python project on Replit, copy your script into the editor, and hit run to see how your guessing game works in real-time!

Step 2: Import Necessary Modules

To generate random numbers, Python offers a built-in module called random. We need to import it at the beginning of our script.

 

import random

 

Step 3: Generate a Random Number

Now, let's use the random module to generate a number between 1 and 100. We’ll store this number in a variable called secret_number.

 

secret_number = random.randint(1, 100)

 

random.randint is a function that returns a random integer within the specified range.

 

Step 4: Ask the User to Guess the Number

We’ll use a loop to let the user guess multiple times. The loop will continue until the user guesses the correct number. Inside the loop, we use the input() function to get the user’s guess and convert it to an integer since input() returns a string.

 

while True:

    guess = int(input("Guess the number between 1 and 100: "))

 

Step 5: Check the Guess

Within the loop, we need to check if the user’s guess is higher, lower, or equal to the secret_number. We’ll give them a hint after each guess.

 

  if guess < secret_number:

        print("Too low, try again!")

  elif guess > secret_number:

        print("Too high, try again!")

  else:

        print("Congratulations! You guessed it right!")

        break

 

Step 6: Keep the Game Running

The while True loop will keep the game running until the correct guess is made. Once the correct number is guessed, we use break to exit the loop.

 

Congrats, You’ve Made a Game!

And just like that, you’ve created a number guessing game in Python! You can run this script in your Python environment, and try guessing the number yourself. It’s a fun way to test your logical thinking and get comfortable with basic Python concepts.

 

Number Guessing Game

Full Guessing Game Code

import random

secret_number = random.randint(1, 100)

while True:

       guess = int(input("Guess the number between 1 and 100: "))

       if guess < secret_number:

                print("Too low, try again!")

       elif guess > secret_number:

                print("Too high, try again!")

       else:

                print("Congratulations! You guessed it right!")

                break

 

Recommendation

The Rundown AIGet the rundown on the latest developments in AI before everyone else.

Ready to Level Up Your Python Skills?

If you enjoyed making this game and want to learn more about Python and other fun projects, why not subscribe to our newsletter? You’ll get access to tons of resources, tips, and a free Python cheat sheet to boost your coding skills!

 

🌟Subscribe Now and Get Your Free Python Cheat Sheet!

 

Jump in and start having fun with Python! Happy coding!