To make the guessing game harder, you can implement several changes:
Increase the Range: Allow the user to guess a number from a larger range, such as 1 to 100.
number = random.randint(1, 100)Limit the Number of Guesses: Set a maximum number of attempts. If the user exceeds this limit, they lose the game.
max_attempts = 5 attempts = 0Then, modify the loop to check the number of attempts:
while guess != number and attempts < max_attempts: guess = int(input("Guess a number between 1 and 100: ")) attempts += 1 if guess < number: print("Too low!") elif guess > number: print("Too high!")Provide Less Feedback: Instead of saying "Too low!" or "Too high!", you could just say "Try again!" to reduce hints.
Add a Timer: Introduce a time limit for guessing. You can use the
timemodule to track how long the user takes.
Here’s an example of the modified code:
import random
number = random.randint(1, 100)
guess = 0
max_attempts = 5
attempts = 0
while guess != number and attempts < max_attempts:
guess = int(input("Guess a number between 1 and 100: "))
attempts += 1
if guess < number:
print("Try again!")
elif guess > number:
print("Try again!")
if guess == number:
print(f"Congratulations! You guessed the number {number}!")
else:
print(f"Sorry! The number was {number}.")
These changes will make the game more challenging and engaging!
