Python Number Guess Game Project Made Easy – In this Python Project for beginners we will explain the code and logic of the popular number guessing game program in Python programming language.

## How Python Number Guessing Project Works

### Import Python Modules :- random and math

First of all we will include some Python modules which are necessary

to write a number guess program in Python.

import random import math

### Get input from the user

In this step we will get input that is the range of numbers to guess from.

# Get Inputs from user for lower bound and upper bound limits lower_bound = int(input("Enter Lower bound of range(Integer number): ")) # Taking upper_bound of range upper_bound = int(input("Enter Upper bound of range(Integer number):- "))

### Code for Random Number Generation – Python Number Guess Game

# code to generate random number between # the lower bound and upper bound guess_num = random.randint(lower_bound, upper_bound)

### Python instructions to get Maximum Try using log function

Here we will calculate the number of tries to guess the number. The log base 2 function

will provide us the total number of attempts to guess the number in game.

max_try = round(math.log(upper_bound - lower_bound + 1, 2)) print("\n\t Kindly note that, You've only ", max_try, " chances to guess the number!\n")

### Define a Count variable and initialize it to zero

count variable will store how many number of attempts acomplished by the user so far.

# count of user guess. count = 0

### Start the loop from 1 to maximum number of attempts

In this while loop we will get the guess from the user. We will compare this guess with

the actual number. An if statement will check if the guess is equal to the actual number

, less than or greater than the actual number. This will cause to display a message to the user

accordingly. For example, if the actual number to guess is 5 and user enters 7, we will display

a message “Your guess is too high”.capitalize So that the user will input a number less than 7 next time.

while count < max_try: count += 1 # get the guessing number from the user guess = int(input("Please Guess a number:- ")) # check if guess number is correct or not if guess_num == guess: print("Success :- Congratulations you guessed the number in ", count, " try") # break the loop you are done break # if guess is greater than actual number elif guess_num > guess: print("Sorry Dear:- You guessed too small! Try again:-") # if guess is greater than actual number elif guess_num < guess: print("Sorry Dear:-You Guessed too high! Try again:-") # If Guessing is more than required guesses, # show a message like:- No try left. if count >= max_try: print("\nSorry you have no Try left") print("\nThe number was %d" % guess_num) print("\tDear:- Better Luck Next time!")

## Python Number Guess Game ProjectÂ – Sample Output

Enter Lower bound of range(Integer number): 1 Enter Upper bound of range(Integer number):- 10 Kindly note that, You've only 3 chances to guess the number! Please Guess a number:- 5 Sorry Dear:- You guessed too small! Try again:- Please Guess a number:- 7 Sorry Dear:- You guessed too small! Try again:- Please Guess a number:- 9 Sorry Dear:- You guessed too small! Try again:- Sorry you have no Try left The number was 10 Dear:- Better Luck Next time!

Another Sample Run

Enter Lower bound of range(Integer number): 1 Enter Upper bound of range(Integer number):- 5 Kindly note that, You've only 2 chances to guess the number! Please Guess a number:- 3 Sorry Dear:- You guessed too small! Try again:- Please Guess a number:- 4 Success :- Congratulations you guessed the number in 2 try

## Code for Python Number Guess Game Project

import random import math # Get Inputs from user for lower bound and upper bound limits lower_bound = int(input("Enter Lower bound of range(Integer number): ")) # Taking Inputs upper_bound = int(input("Enter Upper bound of range(Integer number):- ")) # code to generate random number between # the lower bound and upper bound guess_num = random.randint(lower_bound, upper_bound) max_try = round(math.log(upper_bound - lower_bound + 1, 2)) print("\n\t Kindly note that, You've only ", max_try, " chances to guess the number!\n") # count of user guess. count = 0 # for calculation of minimum number of # guesses depends upon range while count < max_try: count += 1 # get the guessing number from the user guess = int(input("Please Guess a number:- ")) # check if guess number is correct or not if guess_num == guess: print("Success :- Congratulations you guessed the number in ", count, " try") # break the loop you are done break # if guess is greater than actual number elif guess_num > guess: print("Sorry Dear:- You guessed too small! Try again:-") # if guess is greater than actual number elif guess_num < guess: print("Sorry Dear:-You Guessed too high! Try again:-") # If Guessing is more than required guesses, # show a message like:- No try left. if count >= max_try: print("\nSorry you have no Try left") print("\nThe number was %d" % guess_num) print("\tDear:- Better Luck Next time!")