Date: Exp. No.: Page No.
PROJECT NAME: Number Guessing Game with Difficulty Levels
AIM:
To write a program on Number Guessing game with difficultyAllow players to select
difficulty levels that change the range of numbers and the number of guesses available.
DESCRIPTION:
1. Imports and Random Number Generation:
The game begins by importing the random module, which is used to select
a random number from a specified range.
A list of numbers from 0 to 50 is created, and a random number
(choose_num) is selected from this list.
2. Function Definitions:
no_guesses(attempts, i):
This function checks how many attempts the player has left. If there
are attempts remaining, it prompts the player to guess again and
informs them of the remaining attempts. If no attempts are left, it
indicates that the player has lost the game.
easy_hard(choose_num, attempts):
This function handles the main game logic. It informs the player of
the number of attempts they have and enters a loop where the
player can make guesses.
For each guess, the function checks if the guess is too high, too low,
or correct:
If the guess is too high, it prompts the player accordingly and
calls no_guesses to inform them of remaining attempts.
If the guess is too low, it gives a similar response.
If the guess is correct, it congratulates the player and exits the
loop.
3. Game Loop:
PYTHON PROGRAMMING CSEDEPT
Date: Exp. No.: Page No.:
The game starts with a prompt for the player to choose a difficulty level.
The player can input either "easy" or "hard":
If "easy" is chosen, the player has 10 attempts to guess the number.
If "hard" is chosen, the player has 5 attempts.
If the player enters an invalid input, they are prompted to try again.
TEAM MEMBERS:
S.PRAVEENA
ROLL NO:2319A0515
K.V.NANDAPRIYA
ROLL NO:23191A0507
KAVYA SHREE
TEAM MEMBERS ROLE:
S.PRAVEENA:
Develop the no_guesses and easy_hard functions, ensuring they handle user
input and provide appropriate feedback.
KV NANDAPRIYA:
Error detection and Game level management
KAVYA SHREE:
Use efficient algorithms to handle the game's operations
Design the code with clear indentation and logical grouping.
PYTHON PROGRAMMING CSEDEPT
Date: Exp. No.: Page No.:
PROGRAM:
import random
def no_guesses(attempts,i):
if attempts - i != 0:
print('guess again')
return (f"you have {attempts - i} attempts remaining to guesses the number")
else:
return ('you are out of guesses,you lose!!')
def easy_hard(choose_num,attempts):
print(f"you have {attempts} attempts remaining to guess the number!")
for i in range(1,attempts + 1):
guess = int(input('make a guess'))
if guess > choose_num:
print('your guess is too high')
print(no_guesses(attempts,i))
elif guess < choose_num:
print('your guess is too low')
print(no_guesses(attempts,i))
else:
print(f'you guess is right...The answer was {choose_num}')
break
print('let me think of a number between 1 to 50.')
num_list = list(range(51))
choose_num = random.choice(num_list)
PYTHON PROGRAMMING CSEDEPT
Date: Exp. No.: Page No.:
while True:
level = input("Choose level of difficulty...Type 'easy' or 'hard' : ")
if level == 'easy':
easy_hard(choose_num,attempts = 10)
break
elif level == 'hard':
easy_hard(choose_num,attempts = 5)
break
else:
print("you've entered wrong input,Try again")
OUTPUT:
let me think of a number between 1 to 50.
Choose level of difficulty...Type 'easy' or 'hard' : easy
you have 10 attempts remaining to guess the number!
make a guess 45
your guess is too high
guess again
you have 9 attempts remaining to guesses the number
make a guess 30
your guess is too high
guess again
you have 8 attempts remaining to guesses the number
make a guess 20
your guess is too low
guess again
PYTHON PROGRAMMING CSEDEPT
Date: Exp. No.: Page No.:
you have 7 attempts remaining to guesses the number
make a guess 26
your guess is too low
guess again
you have 6 attempts remaining to guesses the number
make a guess 28
your guess is too high
guess again
you have 5 attempts remaining to guesses the number
make a guess 27
you guess is right...The answer was 27
CONCLUSION:
The number guessing game project serves as an engaging and educational exercise in
Python programming, allowing participants to apply fundamental concepts such as
loops, conditionals, functions, and user input handling.
PYTHON PROGRAMMING CSEDEPT
Date: Exp. No.: Page No.:
PYTHON PROGRAMMING CSEDEPT