Guess The Number – Simple Game Using Python

This game is surely not going to be the fanciest game. But any beginner with a basic knowledge of the Python programming language can built-it and understand it easily. I think this can be a good and simple project for a beginner to practice.

By the way, this game has no fancy GUI. You have to play it on your python console/terminal.

So let’s get into the details.

Here, the concept is pretty simple. A function will decide a random number between 1 to 10. And we have to pick that number using only three attempts. Otherwise, we will lose. Simple, right?

You can change the number range and the number of attempts. It’s up to you.

To decide the random number, we will use the random module. It’s a Python built-in module. To import it into our program, we will use:

import random

And then, we have to declare the range. For that, there is a method in the random module called randint. We can provide the number range as a parameter in this method. This method returns a random number from that range.

import random
number = random.randint(1, 10) # both are included
print(number) # don't print it in the guess game

Now we have to guess that number within three attempts. And that’s why we will use a loop, which will run three times.

Inside the loop, we will take input from the console. By default, the input function takes a value as a string. So we have to convert it into an integer. By the way, we can pass a message into the input function to give a hint.

pick = input("Pick a number from 1 to 10: ")
print(pick)
pick = int(pick)
print(pick)

After that, we will compare our input with that random number. If that number is greater than our input, we will give a message that You Picked Lower Number. Which is mainly helps to pick the correct number in future attempts. On the other hand, if the number is smaller than our input, we will give the opposite message. If both conditions are false, that means we’ve got our answer. We have guessed the correct number.

If we can’t guess the correct number within three times, we will print a message at the end of the loop specifying the correct number. Before that, we need to keep track of whether we can correctly guess the number or not. Because if we already guessed it, there is no need to print this message.

Let’s see the entire code.

import random
number = random.randint(1, 10)

win = False

for i in range(3):
    pick = int(input("Guess The Number: "))
    
    if number > pick:
        print("You Picked Lower Number")
    elif number < pick:
        print("You Picked Higher Number")
    else:
        print("You Win!")
        win = True
        break

if not win:
    print(f"No worries. Will win next time. The correct number is: {number}")

I hope you have run the above code successfully. And you played the game and successfully guessed the number. By the way, you can also change the range and attempts number as per your preference.

You can write the game in many ways. You can modify it and add something new to it. It’s up to you.

Best of luck.