0% found this document useful (0 votes)
31 views

D.Y. Patil Technical Campus, Talsande Faculty of Engineering & Faculty of Management (Polytechnic)

Uploaded by

patilshubhamww
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

D.Y. Patil Technical Campus, Talsande Faculty of Engineering & Faculty of Management (Polytechnic)

Uploaded by

patilshubhamww
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

D.Y.

PATIL TECHNICAL CAMPUS, TALSANDE


FACULTY OF ENGINEERING & FACULTY OF
MANAGEMENT
(Polytechnic)

A Micro project Report On


“Number Guessing Game using Python”

Submitted By
Enrollment No. Name

2112200051 Sanskar Mandar Kulkarni

Guided By
Miss. Gurav J.N.

D.Y. PATIL TECHNICAL CAMPUS, TALSANDE FACULTY


OF ENGINEERING & FACULTY OF MANAGEMENT
(Polytechnic)

DEPARTMENT OF COMPUTER ENGINEERING


SEMESTER VI
CERTIFICATE

This is Certify that student of Computer Engineering has


successfully completed the project term work “Number Guessing
Game using Python” in partial fulfilment of the Diploma of
Engineering in Computer as laid down during academic year 2023-
24.

Roll No. Name of Student Exam Seat No.


3216 Sanskar Mandar Kulkarni

Miss. Gurav J.N. Mr. Kumbhar R.S.


Project Guide HoD

Dr. S.R.Pawaskar
Principal

Date: / /2024
Place: Talsande
INDEX

Sr No. Title Page no.

1 Introduction 1

2 Number Guessing Game 2


using Python

3 What we are Gonne do? 3-6

4 Program Code 7-9

5 Output 10

6 Conclusion 11

7 References 12
Number Guessing Game using Python

Introduction

In this article and video, you will learn how to write a simple Guess-the-number
game in Python using a normal text editor this tutorial is meant to be an easy Python project
for beginners, so don’t worry if you don’t understand everything at first. The main point is to
see that code is just text you can watch the video tutorial below, and/or continue reading this
blog for the written tutorial. Now let’s go build and run your first interactive Python project!

Install Python on your computer

If you don’t already have Python installed on your computer, visit Python.org to
download it. This tutorial is taught for UNIX-based systems (Mac & Linux). If you are on
Windows and you don’t have Python pre-installed, you can also build the game in an online
coding interface, such as repl.it.

Build the Game

The first step in building your Python project for beginners game is to write your code in a
text editor.

Coding in your text editor

Open up any Text Editor – this can be as simple as the built-in TextEdit program on MacOS:

D.Y. PATIL TECHNICAL CAMPUS, TALSANDE FACULTY OF ENGINEERING &


FACULTY OF MANAGEMENT(Polytechnic) Page 1
Number Guessing Game using Python

Number Guessing Game using Python

To create a guessing game, we need to write a program to select a random number


between 1and 10. To give hints to the user, we can use conditional statements to tell the user
if the guessed number is smaller, greater than or equal to the randomly selected number if the
guessed number is lower than the randomly selected number, the user will see “too low”. If the
guessed number is higher than the randomly selected number, the user will see “too high”.
When the user guesses the correct number, “you guessed it right!!” will be displayed in the
output.

So, this is how you can write a program to create a guessing game using Python. It is a
popular game among programmers. In this game, the program selects a random number
between two numbers, and the user guesses the correct number. I hope you liked this article on
how to create a guessing game using Python. Feel free to ask valuable questions in the
comments section below.

D.Y. PATIL TECHNICAL CAMPUS, TALSANDE FACULTY OF ENGINEERING &


FACULTY OF MANAGEMENT(Polytechnic) Page 2
Number Guessing Game using Python

What we are Gonne do?

The random function generates the number between 1 to 100. The user has 10 chances
to guess the number. So, we ask max 10 times to user to enter a number. Once the user guess
is matched with an actual number then the game is over. Every time user also sees how many
no. of chances are left. If the user can’t guess the number then the game is over. Simple!!

Which Python concepts are covered?

• If-else
• While Loop
• Random function
• break statement

D.Y. PATIL TECHNICAL CAMPUS, TALSANDE FACULTY OF ENGINEERING &


FACULTY OF MANAGEMENT(Polytechnic) Page 3
Number Guessing Game using Python

Task: Below are the steps:

• Build a Number guessing game, in which the user selects a range.


• Let’s say User selected a range, i.e., from A to B, where A and B belong to Integer.
• Some random integer will be selected by the system and the user has to guess that
integer in the minimum number of guesses

Analysis:
Explanation 1: If the User inputs range, let’s say from 1 to 100. And compiler
randomly selected 42 as the integer. And now the guessing game started, so the user entered
50 as his/her first guess. The compiler shows “Try Again! You guessed too high”. That’s
mean the random number (i.e., 42) doesn’t fall in the range from 50 to 100. That’s the
importance of guessing half of the range. And again, the user guesses half of 50 (Could you
tell me why?). So the half of 50 is 25. The user enters 25 as his/her second guess. This time
compiler will show, “Try Again! You guessed too small”. That’s mean the integers less than
25 (from 1 to 25) are useless to be guessed. Now the range for user guessing is shorter, i.e.,
from 25 to 50. Intelligently! The user guessed half of this range, so that, user guessed 37 as
his/her third guess. This time again the compiler shows the output, “Try Again! You
guessed too small”. For the user, the guessing range is getting smaller by each guess. Now,
the guessing range for user is from 37 to 50, for which the user guessed 43 as his/her fourth
guess.

This time the compiler will show an output “Try Again! You guessed too high”. So,
the new guessing range for users will be from 37 to 43, again for which the user guessed the
half of this range, that is, 40 as his/her fifth guess. This time the compiler shows the output,
“Try Again! You guessed too small”. Leaving the guess even smaller such that from 41 to
43. And now the user guessed 41 as his/her sixth guess. Which is wrong and shows output
“Try Again! You guessed too small”. And finally, the User Guessing the right number which
is 42 as his/her seventh guess.

D.Y. PATIL TECHNICAL CAMPUS, TALSANDE FACULTY OF ENGINEERING &


FACULTY OF MANAGEMENT(Polytechnic) Page 4
Number Guessing Game using Python

Total Number of Guesses = 7


Explanation 2: If the User inputs range, let’s say from 1 to 50. And compiler
randomly selected 42 as the integer. And now the guessing game started. So the half of 50 is
25. The user enters 25 as his/her First guess. This time compiler will show, “Try Again! You
guessed too small”. That’s mean the integers less than 25 (from 1 to 25) are useless to be
guessed. Now the range for user guessing is shorter, i.e., from 25 to 50. Intelligently! User
guessed half of this range, so that, user guessed 37 as his/her second guess. This time again
the compiler shows the output, “Try Again! You guessed too small”. For the user, the
guessing range is getting smaller by each guess. Now, the guessing range for user is from 37
to 50, for which the user guessed 43 as his/her third guess.

This time the compiler will show an output “Try Again! You guessed too high”. So,
the new guessing range for users will be from 37 to 43, again for which the user guessed the
half of this range, that is, 40 as his/her fourth guess. This time the compiler shows the
output, “Try Again! You guessed too small”. Leaving the guess even smaller such that from
41 to 43. And now the user guessed 41 as his/her fifth guess. Which is wrong and shows
output “Try Again! You guessed too small”. And finally, the User Guessed the right number
which is 42 as his/her sixth guess.
Total Number of Guesses = 6

D.Y. PATIL TECHNICAL CAMPUS, TALSANDE FACULTY OF ENGINEERING &


FACULTY OF MANAGEMENT(Polytechnic) Page 5
Number Guessing Game using Python

Algorithm: Below are the Steps:

• User inputs the lower bound and upper bound of the range.
• The compiler generates a random integer between the range and store it in a variable
for future references.
• For repetitive guessing, a while loop will be initialized.
• If the user guessed a number which is greater than a randomly selected number, the
user gets an output “Try Again! You guessed too high“
• Else If the user guessed a number which is smaller than a randomly selected number,
the user gets an output “Try Again! You guessed too small”
• And if the user guessed in a minimum number of guesses, the user gets a
“Congratulations! ” Output.
• Else if the user didn’t guess the integer in the minimum number of guesses, he/she
will get “Better Luck Next Time!” output.

D.Y. PATIL TECHNICAL CAMPUS, TALSANDE FACULTY OF ENGINEERING &


FACULTY OF MANAGEMENT(Polytechnic) Page 6
Number Guessing Game using Python

Program Code

import random

import math

# Taking Inputs

lower = int(input("Enter Lower bound:- "))

# Taking Inputs

upper = int(input("Enter Upper bound:- "))

# generating random number between

# the lower and upper

x = random.randint(lower, upper)

print("\n\tYou've only ",

round(math.log(upper - lower + 1, 2)),

" chances to guess the integer!\n")

# Initializing the number of guesses.

count = 0

D.Y. PATIL TECHNICAL CAMPUS, TALSANDE FACULTY OF ENGINEERING &


FACULTY OF MANAGEMENT(Polytechnic) Page 7
Number Guessing Game using Python

# for calculation of minimum number of

# guesses depends upon range

while count < math.log(upper - lower + 1, 2):

count += 1

# taking guessing number as input

guess = int(input("Guess a number:- "))

# Condition testing

if x == guess:

print("Congratulations you did it in ",

count, " try")

# Once guessed, loop will break

break

elif x > guess:

print("You guessed too small!")

elif x < guess:

print("You Guessed too high!")

D.Y. PATIL TECHNICAL CAMPUS, TALSANDE FACULTY OF ENGINEERING &


FACULTY OF MANAGEMENT(Polytechnic) Page 8
Number Guessing Game using Python

# If Guessing is more than required guesses,

# shows this output.

if count >= math.log(upper - lower + 1, 2):

print("\nThe number is %d" % x)

print("\tBetter Luck Next time!")

# Better to use This source Code on pycharm!

D.Y. PATIL TECHNICAL CAMPUS, TALSANDE FACULTY OF ENGINEERING &


FACULTY OF MANAGEMENT(Polytechnic) Page 9
Number Guessing Game using Python

OUTPUT

D.Y. PATIL TECHNICAL CAMPUS, TALSANDE FACULTY OF ENGINEERING &


FACULTY OF MANAGEMENT(Polytechnic) Page 10
Number Guessing Game using Python

Conclusion

The foundation of the number guessing game in Python is the player's assumption that
they can guess a number within the given range. The player wins the game if they correctly
guess the target number; else, they lose.

D.Y. PATIL TECHNICAL CAMPUS, TALSANDE FACULTY OF ENGINEERING &


FACULTY OF MANAGEMENT(Polytechnic) Page 11
Number Guessing Game using Python

Reference

www.google.com
https://codingnomads.co/blog/python
https://thecleverprogrammer.com/number-guessing-game-using-python/
https://www.geeksforgeeks.org

D.Y. PATIL TECHNICAL CAMPUS, TALSANDE FACULTY OF ENGINEERING &


FACULTY OF MANAGEMENT(Polytechnic) Page 12

You might also like