0% found this document useful (0 votes)
32 views3 pages

Guessing Game

The document describes a Python program that creates a guessing game where the player tries to guess a randomly generated number between 1 and 100. The program imports the random module, defines a Notvalid exception, and creates a GuessingGame class to generate a random number and check guesses. It then creates an instance of the class, prompts the user to enter guesses, and checks if the guess is correct, printing the number of guesses if correct.

Uploaded by

Chirag Chaudhary
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)
32 views3 pages

Guessing Game

The document describes a Python program that creates a guessing game where the player tries to guess a randomly generated number between 1 and 100. The program imports the random module, defines a Notvalid exception, and creates a GuessingGame class to generate a random number and check guesses. It then creates an instance of the class, prompts the user to enter guesses, and checks if the guess is correct, printing the number of guesses if correct.

Uploaded by

Chirag Chaudhary
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/ 3

Guessing Game

Description :

This is a Python program that creates a guessing game where the player tries to guess a
randomly generated number between 1 and 100.

The program starts by importing the random module to generate a random integer
between 1 and 100.

Next, the program defines a custom exception Notvalid which is raised when the user
enters a number that is not between 1 and 100.

After that, the program defines a GuessingGame class that has an __init__ method which
initializes the randomly generated number and the number of guesses to 0.

The GuessingGame class also has a guess method that takes a number as an argument and
returns a string based on whether the number is higher or lower than the randomly
generated number. If the number is within 5 of the randomly generated number, the string
returned will be "Little high" or "Little low". If the number is further away, the string
returned will be "Too high" or "Too low". If the number is equal to the randomly generated
number, the string returned will be "Correct!".

Finally, the program creates an instance of the GuessingGame class and starts a loop
where the user is prompted to guess the number. If the user enters a number that is not
between 1 and 100, the program raises the Notvalid exception. If the user guesses the
correct number, the program prints a congratulatory message and the number of guesses it
took to guess the correct number.

Code:
import random

class Notvalid(Exception):

pass

class GuessingGame:
def __init__(self):

self.number = random.randint(1, 100)

self.guesses = 0

def guess(self, number):

self.guesses += 1

if(number<0 or number>100):

raise Notvalid("Please enter the number between 1 to 100 !")

else:

if number < self.number:

if(number>self.number-5):

return "Little Low"

else:

return "Too low"

elif number > self.number:

if(number<self.number+5):

return "Little high"

else:

return "Too high"

else:

return "Correct!"

game = GuessingGame()

print("I'm thinking of a number between 1 and 100. Can you guess it?")
while True:

guess = int(input("Enter your guess: "))

try:

result = game.guess(guess)

print(result)

if result == "Correct!":

print("Congratulations, you guessed the number in", game.guesses, "tries!")

break

except Notvalid as n:

print("Notvalid :",n)

Output :

You might also like