From 3fd22d2380f418864b09bac34e0b1d2dd1cbc131 Mon Sep 17 00:00:00 2001 From: Debasish Pradhan Date: Wed, 10 Jun 2026 11:08:13 +0530 Subject: [PATCH] Add attempt limit to guessing game Limit the number of attempts to guess the secret number. --- examples/guessing_game.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/guessing_game.py b/examples/guessing_game.py index 5c27a5443f..1d7bb3aeec 100644 --- a/examples/guessing_game.py +++ b/examples/guessing_game.py @@ -5,7 +5,9 @@ def guessing_game(): secret_number = random.randrange(101) - while True: + attempts = 3 + + while attempts > 0: guess = input("Input your guess: ") try: @@ -23,6 +25,10 @@ def guessing_game(): print("Too small!") else: print("Too big!") + attempts -= 1 + if attempts == 0: + print(f"You have {attempts} left!") + break if __name__ == '__main__': - guessing_game() \ No newline at end of file + guessing_game()