How to build a Guess the Number Game in Python
Rishabh Singh β‘
Posted on February 20, 2021
Hello everyone, today we are going to build a Guess the Number Game in Python!
How does it work?
Our games will randomly generate a number between 0 and 30 and the player has to guess the number.
If the number entered by the player is less than generated number then the player will be prompted with too low
message!
And if the number entered by the player is more than generated number then the player will be prompted with too high
message!
This process will be repeated until the player finds the right number.
Pretty straightforward right... let's do it!
Let's Code
Now since we are going to generate a random number and if you've been following past tutorials in the series than you might know what we are talking about here. We are going to import a module that comes pre-installed with our Python, it's called random
.
Let's import it into our project.
import random
Now we have to initialize a max_num
variable. So that we can customize the difficulty as per our choice. Higher the value of max_num
, the higher the difficulty. For now, let's keep it 30.
max_num = 30
Now it's time we generate our random number which the player has to find.
For that we will be using randint()
function from the random
module. We will store this random value in random_number
variable.
random_number = random.randint(1, max_num)
It will generate a random number between 1
& max_num
.
Then we will also initialize a guess
variable to store the player's answer for comparison.
guess = 0
Now let's create a while loop to keep asking the player until the right number is found. We will keep this loop running until our random_number
& guess matches.
while guess != random_number:
pass
Alright, so it's time we ask our player to make a guess enter a number to start the game.
while guess != random_number:
guess = int(input(f"Guess the number between 1 & {max_num}: "))
We will also use int()
function to covert the number from a string into an integer so that we can compare it to check the answer.
Now we will make use of if
conditionals to compare the answer and provide suitable feedback to the player.
while guess != random_number:
guess = int(input(f"Guess the number between 1 & {max_num}: "))
if guess < random_number:
print("Wrong! Too low...")
elif guess > random_number:
print("Wrong! Too high...")
Hopefully, the logic of the while loop will be clear as we already discussed it in How does it work? section.
Here are our while
loop ends.
Now one last thing we have to do is to print a final message to the screen if the player got the right answer.
print(f"Thats Right! Random number is {random_number}")
Let's summarize the logic one again:
-
max_num
variable will decide the difficulty of our game. Higher the value, the higher the difficulty. - We will use
random.randint(1, max_num)
function to generate a random number. -
guess
variable will contain the answer entered by the player. - Now loop will begin and if the number entered by the player matches the generated answer then the loop will no longer execute and the final
print()
statement will be printed, telling the player that the game is over. - Otherwise, the loop will keep running until the right number is entered by the player.
Source Code
You can find the complete source code of this project here -
mindninjaX/Python-Projects-for-Beginners
Support
Thank you so much for reading! I hope you found this beginner project useful.
If you like my work please consider Buying me a Coffee so that I can bring more projects, more articles for you.
Also if you have any questions or doubts feel free to contact me on Twitter, LinkedIn & GitHub. Or you can also post a comment/discussion & I will try my best to help you :D
Posted on February 20, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.