Untitled

 avatar
unknown
plain_text
2 months ago
1.1 kB
2
Indexable
import random

def guess_the_number():
    print("Welcome to 'Guess the Number'!")
    print("I'm thinking of a number between 1 and 100.")

    # Generate a random number between 1 and 100
    number_to_guess = random.randint(1, 100)

    attempts = 0
    guessed_correctly = False

    while not guessed_correctly:
        try:
            # Get the player's guess
            player_guess = int(input("Enter your guess: "))

            # Increment the number of attempts
            attempts += 1

            # Check if the player's guess is too low, too high, or correct
            if player_guess < number_to_guess:
                print("Too low! Try again.")
            elif player_guess > number_to_guess:
                print("Too high! Try again.")
            else:
                guessed_correctly = True
                print(f"Congratulations! You guessed the number {number_to_guess} in {attempts} attempts.")

        except ValueError:
            print("Please enter a valid number.")

# Run the game
if __name__ == "__main__":
    guess_the_number()
Leave a Comment