import random
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
attempts = 0
print("Welcome to the Guess the Number game!")
print("I'm thinking of a number between 1 and 100.")
while True:
try:
guess = int(input("Take a guess: "))
attempts += 1
if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the number {secret_number} in {attempts} attempts!")
break
except ValueError:
print("Invalid input. Please enter a valid number.")
Welcome to the Guess the Number game!
I'm thinking of a number between 1 and 100.
Too high! Try again.
Too high! Try again.
Too high! Try again.
Too low! Try again.
Too low! Try again.
Too low! Try again.
Congratulations! You guessed the number 20 in 7 attempts!