From 6b369962aca0cd22e15967e2838a6584ee2e3bf0 Mon Sep 17 00:00:00 2001 From: Indrani Som Date: Sat, 28 Oct 2023 16:49:01 +0530 Subject: [PATCH] added a number guessing game --- numberguessinggame/index.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 numberguessinggame/index.py diff --git a/numberguessinggame/index.py b/numberguessinggame/index.py new file mode 100644 index 00000000000..3116af47dce --- /dev/null +++ b/numberguessinggame/index.py @@ -0,0 +1,28 @@ +import random + +def number_guessing_game(): + + secret_number = random.randint(1, 100) + attempts = 0 + + print("Welcome to the Number Guessing Game!") + print("I'm thinking of a number between 1 and 100.") + + while True: + try: + guess = int(input("Your guess: ")) + attempts += 1 + + if guess < secret_number: + print("Too low! Try again.") + elif guess > secret_number: + print("Too high! Try again.") + else: + print(f"Congratulations! You guessed the number {secret_number} in {attempts} attempts!") + break + + except ValueError: + print("Please enter a valid number.") + +if __name__ == "__main__": + number_guessing_game()