import random

def guessing_game():
    number_to_guess = random.randint(1, 10)
    attempts = 0

    print("Welcome to the Guessing Game!")
    print("I have chosen a number between 1 and 10. Try to guess it.")

    while True:
        user_guess = int(input("Your guess: "))
        attempts += 1

        if user_guess == number_to_guess:
            print(f"Congratulations! You guessed the number in {attempts} attempts.")
            break
        elif user_guess < number_to_guess:
            print("Too low. Try again.")
        else:
            print("Too high. Try again.")

if __name__ == "__main__":
    guessing_game()

Post a Comment

0 Comments