diff --git a/guess_number.py b/guess_number.py index bc76576..c24ec94 100644 --- a/guess_number.py +++ b/guess_number.py @@ -1,24 +1,32 @@ import random def guess_number(): - number_to_guess = random.randint(1, 100) # Random number between 1 and 100 + print("Welcome to the Number Guessing Game!") + lower_bound = int(input("Enter the lower bound of the range: ")) + upper_bound = int(input("Enter the upper bound of the range: ")) + + number_to_guess = random.randint(lower_bound, upper_bound) # Random number within user-defined range attempts = 0 guessed = False - print("Welcome to the Number Guessing Game!") - print("I have chosen a number between 1 and 100. Can you guess it?") + print(f"I have chosen a number between {lower_bound} and {upper_bound}. Can you guess it?") while not guessed: - user_guess = int(input("Enter your guess: ")) - attempts += 1 + try: + user_guess = int(input("Enter your guess: ")) + attempts += 1 - if user_guess < number_to_guess: - print("Too low! Try again.") - elif user_guess > number_to_guess: - print("Too high! Try again.") - else: - print(f"Congratulations! You guessed the number in {attempts} attempts.") - guessed = True + if user_guess < lower_bound or user_guess > upper_bound: + print(f"Please guess a number between {lower_bound} and {upper_bound}.") + elif user_guess < number_to_guess: + print("Too low! Try again.") + elif user_guess > number_to_guess: + print("Too high! Try again.") + else: + print(f"Congratulations! You guessed the number in {attempts} attempts.") + guessed = True + except ValueError: + print("Invalid input. Please enter a valid integer.") # Start the game guess_number()