Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
850 B
1
Indexable
Never
import random

def guess_the_number():
    lower_limit = 1
    upper_limit = 100
    secret_number = random.randint(lower_limit, upper_limit)
    attempts = 0
    
    print(f"Welcome to the Guess the Number game! I've chosen a number between {lower_limit} and {upper_limit}.")
    
    while True:
        try:
            guess = int(input("Enter your 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've guessed the number {secret_number} in {attempts} attempts.")
                break
        except ValueError:
            print("Please enter a valid number.")

if __name__ == "__main__":
    guess_the_number()