DEV Community

Cover image for How to Build a Guessing Number Game Using Python
Sai Ashish
Sai Ashish

Posted on • Originally published at theinsightfulcoder.com

How to Build a Guessing Number Game Using Python

Welcome to Python Projects for Beginners SeriesπŸ™‹πŸ»β€β™‚οΈ

I'm Sai Ashish and today, we are going to build a simple Guessing Number Game using Python. You will be given 3 chances. All you have to do is guess a number between 1 to 10 (included) and if you guess the number correctly, you win πŸ†

Simple enough? Let's get started?πŸš€

Through this project, you're going to learn about:

1. The Random Module
2. Basic concepts of Python: type casting, conditional statements,
   iterative statements and string interpolation
3. Implementation of the guessing the number game with detailed explanation
Enter fullscreen mode Exit fullscreen mode

You can access the basic Python concepts required for this build from hereπŸ’£

Step 1: Modules Required:

To help us build this game, we are going to use an amazing module of Python called Random. The random module generates random numbers for us. This gets in real handy as we do not want our computer to be biased.

Step 2: Time to Code!

The Insightful Coder: Time to Code

The first step is to import the random module:

import random
Enter fullscreen mode Exit fullscreen mode

Now, we want the computer to choose a random number between 1 and 10. To set these limits, we make use of the randint() method of python.

Syntax of randint() is given as:

randint(lower limit , upper limit)

For Our Case:

number = random.randint(1,10)
Enter fullscreen mode Exit fullscreen mode

Let's give the player 3 chances to guess the lucky number. To do that, we would require a loop that would repeat our game 3 times.

for i in range(0,3):
Enter fullscreen mode Exit fullscreen mode

Inside our loop, we would like to ask the player for a number using the input() function. As python accepts string by default, we would convert the string into a number using function int(). The conversion of one data type into another is known as type casting or type conversion.

user = int(input("Guess the lucky number"))
Enter fullscreen mode Exit fullscreen mode

Next, we would compare if the number guessed by the user equals the number generated by the computer. To compare, we use the 'if' statement and check for equality using the == operator.

if user == number:
Enter fullscreen mode Exit fullscreen mode

If the number guessed is correct, we display "Hurray!!" using the print statement.

print("Hurray!!")
Enter fullscreen mode Exit fullscreen mode

We would also like to display the lucky number. We would implement this concept using a mechanism called f-string or literal string interpolation.

Interpolation or interpolate means insert (something of a different nature) into something else. Here, we insert the value of the variables inside the statement of the string we are going to display.

#syntax for f-string or string interpolation
name = 'Ashish'
age = 20
print(f"Hello, My name is {name} and I'm {age} years old."

#output is generated as the value of variable replaced with contents of {}
Hello, My name is Ashish and I'm 20 years old.
Enter fullscreen mode Exit fullscreen mode

In our game, we use interpolation as:

print(f"You guessed the number right, it's {number}")
Enter fullscreen mode Exit fullscreen mode

Now, if the player has 3 unsuccessful attempts, the game gets over and the number is displayed on the screen as:

if user != number:
    print(f"Your guess is incorrect, the number is {number}"
Enter fullscreen mode Exit fullscreen mode

The Final Source Code Is Displayed Below:

Final Source Code for Guessing Game Using Python

The Output if You're Lucky Indeed😍

Output Scenario 2 for Guessing Game Using Python

Well, Better Luck Next Time😒

Output Scenario 2 for Guessing Game Using Python

There you go! You have now built your very own guessing game with just 10 lines of codeπŸ‘©πŸ»β€πŸ’» As a gift for staying till now, you get access to my Python For Beginners Series Repository🎁 This repository contains all the source code you'd need to get started as a Python Developer 🐍

You can also download the source code to this project here. Do hit the twinkle star, if this article provided value to you πŸ”₯

While the game looks simple and boring, it's actually very addictiveπŸ˜…
Want to take it even further? As a challenge, try building a GUI for your gameπŸš€

And while you're at it, consider giving this blog the maximum love you can and I promise to give you such value bombs every week πŸ’£ Until then, take care πŸ™‹πŸ»β€β™‚οΈ

Bonus Insights by The Insightful Coder :

  • Interested in Building Your Own Artificial Intelligence Projects using Python?: Check out the Python AI Series🧠

  • Wanna Discover Some Valuable Tech-Hacks πŸ› ?: Check out the Tech-Hacks for Everybody Series😎

  • I'm also dropping daily value bombs and development insights on my Instagram Page. Make sure to follow me up πŸ’―

  • Find and Download All My Project Source Codes at My Github Repository 🎁#for_our_case

Top comments (2)

 
theinsightfulcoder profile image
Sai Ashish

Anytime πŸ™ŒπŸ»

Collapse
 
theinsightfulcoder profile image
Sai Ashish

Hey Allyedge, I use carbon.now.sh to create my code snippet images. Glad you liked it πŸ’―