D.Y. Patil Technical Campus, Talsande Faculty of Engineering & Faculty of Management (Polytechnic)
D.Y. Patil Technical Campus, Talsande Faculty of Engineering & Faculty of Management (Polytechnic)
Submitted By
Enrollment No. Name
Guided By
Miss. Gurav J.N.
Dr. S.R.Pawaskar
Principal
Date: / /2024
Place: Talsande
INDEX
1 Introduction 1
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!
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.
The first step in building your Python project for beginners game is to write your code in a
text editor.
Open up any Text Editor – this can be as simple as the built-in TextEdit program on MacOS:
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.
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!!
• If-else
• While Loop
• Random function
• break statement
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.
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
• 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.
Program Code
import random
import math
# Taking Inputs
# Taking Inputs
x = random.randint(lower, upper)
count = 0
count += 1
# Condition testing
if x == guess:
break
OUTPUT
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.
Reference
www.google.com
https://codingnomads.co/blog/python
https://thecleverprogrammer.com/number-guessing-game-using-python/
https://www.geeksforgeeks.org