How to make the game harder?

QuestionsQuestions4 SkillsProPython Control StructuresOct, 17 2025
0168

To make the guessing game harder, you can implement several changes:

  1. Increase the Range: Allow the user to guess a number from a larger range, such as 1 to 100.

    number = random.randint(1, 100)
    
  2. 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 = 0
    

    Then, 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!")
    
  3. Provide Less Feedback: Instead of saying "Too low!" or "Too high!", you could just say "Try again!" to reduce hints.

  4. Add a Timer: Introduce a time limit for guessing. You can use the time module 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!

0 Comments

no data
Be the first to share your comment!