Untitled
import random def guess_the_number(): print("Welcome to the Number Guessing Game!") print("I have selected a number between 1 and 100. Try to guess it!") number_to_guess = random.randint(1, 100) attempts = 0 while True: guess = input("Enter your guess: ") # Check if the input is a valid number if not guess.isdigit(): print("Please enter a valid number.") continue guess = int(guess) attempts += 1 if guess < number_to_guess: print("Too low! Try again.") elif guess > number_to_guess: print("Too high! Try again.") else: print(f"Congratulations! You've guessed the number in {attempts} attempts!") break # Run the game guess_the_number()
Leave a Comment