From cbb3a1c3f86d6e9879a256eb6ab20e9304539cae Mon Sep 17 00:00:00 2001 From: shuvomdhar Date: Thu, 2 Jul 2026 16:51:34 +0530 Subject: [PATCH] feat: add number guessing game implementation --- number_guessing.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 number_guessing.py diff --git a/number_guessing.py b/number_guessing.py new file mode 100644 index 00000000000..d1084d22afb --- /dev/null +++ b/number_guessing.py @@ -0,0 +1,30 @@ +import random + +def guessing_game(): + print("Welcome to the Number Guessing Game!") + + upper_bound = int(input("Write the upper bound of the range: ")) + + print(f"I'm thinking of a number between 1 and {upper_bound}.") + + number_to_guess = random.randint(1, upper_bound) + attempts = 0 + guessed_correctly = False + + while not guessed_correctly: + try: + guess = int(input("Make a guess: ")) + attempts += 1 + + if guess < number_to_guess: + print("Too low.") + elif guess > number_to_guess: + print("Too high.") + else: + guessed_correctly = True + print(f"Congratulations! You've guessed the number {number_to_guess} in {attempts} attempts.") + except ValueError: + print("Please enter a valid integer.") + +if __name__ == "__main__": + guessing_game() \ No newline at end of file