# Welcome message print(“Welcome to the Guessing Game!”) print(“I have chosen a number between 1 and 20. Try to guess it!”)
# Generate a random number between 1 and 20 secret_number = random.randint(1, 20)
# Initialize attempt counter attempts = 0
while True: # Increment attempt count attempts += 1
# Take user input try: guess = int(input(“Enter your guess: “)) except ValueError: print(“Invalid input! Please enter a number between 1 and 20.”) continue
# Check if the guess is correct if guess == secret_number: print(f”Congratulations! You guessed it right in {attempts} tries.”) break # Exit the loop once guessed correctly elif guess < secret_number: print(“Too low! Try a higher number.”) elif guess > secret_number: print(“Too high! Try a lower number.”) else: print(“Invalid numbers”)