Python Games For Beginners – Guess the random number Game

Welcome to the series of python games for beginners. In this blog, we will see how code works for the program Guess the random number game in python programming. This blog is going to be interesting as always and the program which we will see in this blog is going to be very-very simple but cool and interesting for beginners of python programming who want to do something cool. So let’s start with Python Games For Beginners – Guess the random number Game.

This program of Guess the random number is a game that will ask the user to guess a random number generated by this program within a given range. If the user will guess the correct number then the program will display ” you won! “.

There are different approaches to code this program but I will show you the below two ways which are a little bit different from each other based upon some criteria.

Guess the random number game in Python

1. First Approach

By this approach, we will write code for this simple game by keeping in mind that the user will get a so-called unlimited number of chances to guess the random number which will be generated by the program in a given range.

Let say we want the number to be generated by the program in the range of 1 to 15. This program will continue until the user will guess the correct number. This means that the user is going to get more than a thousand chances. It’s funny but true. Even the user will start entering the counting numbers from 1 to 15 then it will take a maximum of 15 chances to guess the number. But let’s think about a program that will generate a random number between 1 and 5000. Then you may take 5000 chances to guess the number.

Explanation:

First of all import the random module of python so that this program will use its inbuilt method to generate a random number. Now, we are ready to use methods of the random module. By passing the range values in randint() method, this code will generate a random integer and we are storing that integer in the number variable.

We are creating two different variables, first is your_guess to get the guessed input from the user and the second variable is times which will store the value of your chances, i.e, the number of times you took to guess the correct random number. Initially, we set the value of times = 1 because the user will take at least one chance to guess the random number.

Now apply the while loop because the program will take the input from the user until and unless the user will guess the random number. Thatswhy we use the loop condition as while your_guess != number because this loop will continue to execute if your_guess is not equaled to the random number generated by the program.

your_guess = input ( “guess a number between 1 and 15: ” ) means the input from the user is going to be stored in the variable your_guess and we all know that this is a string type value so we will convert it into integer type by typecasting. So your_guess = int ( your_guess ) is the code where we are changing the your_guess variable from string type to integer type and storing it again in your_guess. Now your_guess variable is containing an integer value. So let’s move now.

If the user’s guess will be equal to the random number, means if the user has guessed the correct number, means if your_guess == number then print (f”You won! in {times} chance ” ).

Else if the user will not guess the correct random number, the program will ask the user to enter the guessing number again by the below code.

else:
print ( “Noooooo. sorry. try again! Don’t worry, you have many chances to guess” )

So the final program will look like this :

import random

number = random.randint ( 1, 15 )
your_guess = None
times=1
while your_guess != number:
    
    your_guess = input ( "guess a number between 1 and 15: " )
    your_guess = int ( your_guess )

    if your_guess == number:
        print (f"You won! in {times} chance " )
        break
    else:
        print ( "Noooooo. sorry. try again! Don't worry, you have many chances to guess" )
        
    times = times+1

Output

python-games-for-beginners-guess-the-number-1

2. Second Approach

By this approach, we will restrict the user from guessing the random number in unlimited chance. As we saw in the first method that the users were getting many chances to guess the number but this time we will give only a few chances to the user to guess the random number.

Explanation :

Let’s make the guess difficult for the users. In this program, we will give only 3 chances to a user to guess the number. So initially we set the value of the variable your_chance = 3.

In the while loop, we use the code while (your_chance > 0) it means that this loop will execute and take input value from the user only 3 times ( for your_chance=3, your_chance=2, your_chance=1). So we restrict the user to guess the number more than 3 times.

So the final program will look like this :

import random

number = random.randint ( 1,15 )
your_chance = 3

while (your_chance > 0):
    your_guess = int ( input ( "Guess the number between 1 and 15: " ) )
    if ( number == your_guess ):
        print ( "You Won! " )
        break
        
    else:
        print ( f"That was wrong. You have {your_chance-1} chance(s) left to guess the correct number : " )
        
    your_chance -= 1
    
if (your_guess != number ):
    print("You Loose!!")

Output

python games for beginners guess the number

Now check the output if the user can’t guess the random number up to 3 times :

python games for beginners guess the number

Conclusion:

Finally, this simple, cool, interesting yet easy game successfully executed and you can run it on your machine to try it by yourself. Read more blogs on this site to learn about some more cool python programs and Interesting python projects with source code. Thank You for reading!..

This Post Has 3 Comments

Leave a Reply