Untitled

 avatar
unknown
plain_text
10 months ago
882 B
1
Indexable
import random

def guess_the_number():
    print("Welcome to the Number Guessing Game!")
    number_to_guess = random.randint(1, 100)
    attempts = 0

    while True:
        try:
            guess = int(input("Guess a number between 1 and 100: "))
            attempts += 1
            
            if guess < 1 or guess > 100:
                print("Please guess a number within the range!")
                continue
            
            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
        except ValueError:
            print("That's not a valid number. Please try again.")

if __name__ == "__main__":
    guess_the_number()
Editor is loading...
Leave a Comment