Guess the Number || Python Basic Game

 Guess the Number Using Python Language

Today we will make a small game using Python Language. Speaking more specifically we will use a random module for making this game. Let's take a sneak-peek how will it work.



Logic:

    We will select any random value from a particular range & store in a variable 'n', then ask the user to input any number & store in variable 'guess', compare the variable 'guess' to the number that is store in the variable 'n'. Using the if-else statement we will compare both number store in variable,

            Case 1: If the variable 'n' is greater than the variable 'guess', that means you need to guess another number but greater than the number store in the variable 'guess'.

            Case 2: If the variable 'guess' is greater than variable 'n', that means you need to guess another number but smaller than the number store in the variable 'guess'.

Code:

    To select any number from the given range we will be using the randint() function that returns an integer value from any given range. Its syntax is:

        randint(start, end) = where start is the starting range, end is the ending range.

We will store the value returning from randint() in the 'n' variable. Now we will ask the user to input any number so in order to start the game. 
import random
#random number will be store in "n" range from 1-50
n = random.randint(1,50)
now we will ask the user to input any number then will start to compare the number entered by the user and the number store in variable 'n'
guess = int(input("Guess the number : "))
if guess<n:
    print("The Number bigger than ",guess)
elif guess>n:
    print("The Number is less than ",guess)
else:
    print("You guess correct")
    break
now to make the user guess the correct number until it's similar to variable 'n' we will place the above code into a loop, here while loop will be the best. Now user has an unlimited chance to guess number until he guesses the correct one.

You can also limit the chance to limit, all you need to do is add a count variable that will count the chance of user & when it reaches a specific number of chances it will get terminated and the user will lose the game.

 Having any problem regarding any programming related stuff, feel free to dm me on my Social Media Handle -->Instagram

Check my Portfolio-->prathmeshchaudhari.me

Subscribe my Blog & comment below !!!

Comments