The “Number Guessing Game” is an engaging Python program that challenges the player to guess a randomly generated number within a specified range. In this game, the player has the option to choose between two difficulty levels: “Easy” and “Hard.” The primary objective is to guess the secret number with a limited number of attempts, promoting strategic thinking and problem-solving. Below is a detailed program flow and solution

Program Flow:

1. The program displays a welcome message and instructions, prompting the player to choose a difficulty level (“Easy” or “Hard”).
2. The player selects a difficulty level, and the program sets the number of lives accordingly (10 for “Easy” and 5 for “Hard”).
3. The program generates a random secret number within the range of 1 to 100.
4. The player is prompted to enter their first guess.
5. The program checks if the guess is valid and within the allowed range. If not, it provides an error message and prompts the player to enter a valid guess.
6. The program compares the guess with the secret number and provides feedback, indicating whether the guessed number is higher or lower. It also decreases the number of lives remaining.
7. Steps 4-6 repeat until one of the game-ending conditions is met.
8. If the player wins, the program displays a congratulatory message, the secret number, and the number of lives used.
9. If the player loses (exhausts all lives), the program reveals the secret number and provides a message indicating that the game is over.
10. The program asks if the player wants to play again. If yes, it generates a new secret number and restarts the game with the selected difficulty level. If no, the program exits.

## Solution

``````import random

HARD_LIVES = 5
EASY_LIVES = 10

def allot_lives():
level = input("Choose a level 'e' for easy or 'h' for hard: ").lower()
if level == 'e':
lives = EASY_LIVES
else:
lives = HARD_LIVES
return lives

if user_guess > com_num:
print("Too High")
elif user_guess < com_num:
print("Too Low")
else:
print("You guessed correctly")
return lives - 1

def guess_game():
print("Welcome to the Number Guessing Game")
lives = allot_lives()
com_num = random.randint(1, 100)

user_guess = 0
while user_guess != com_num:
try:
user_guess = int(input("Guess your number between 1-100: "))
except ValueError:
print("Invalid input. Please enter a valid number.")
continue

if lives == 0:
print(f"You Lost! The correct number was {com_num}")
break
elif user_guess != com_num:
print("Guess Again")

play_again = input("Do you want to play again? (press 'y' for yes or 'n' for no): ").lower()
if play_again == 'y':
guess_game()
else:
print("Thanks for playing!")

guess_game()
``````

Day 12 : Project Number Guessing Game