From 8464257aa45fffb8cb99f591efed785308260257 Mon Sep 17 00:00:00 2001 From: Aidan Hersey Date: Wed, 25 Mar 2026 15:59:06 +0000 Subject: [PATCH 1/2] Add range validation to guessing game input --- guessing-game/guessing-game.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/guessing-game/guessing-game.py b/guessing-game/guessing-game.py index 5ee3093..65ba9c1 100644 --- a/guessing-game/guessing-game.py +++ b/guessing-game/guessing-game.py @@ -17,6 +17,11 @@ # Ask the user for their guess guess = int(input("Enter your guess: ")) + # Ensure that guess is between 1-100 + if guess < 1 or guess > 100: + print("Please enter a number between 1 and 100.\n") + continue + # Check if the guess is correct if guess == secret_number: print("🎉 Congratulations! You guessed the number!") From ce9c96548f5ea0a25a78d6d1ff70d0811c43e6f5 Mon Sep 17 00:00:00 2001 From: Aidan Hersey Date: Wed, 8 Apr 2026 13:35:31 +0100 Subject: [PATCH 2/2] Add replay feature to guessing game --- guessing-game/guessing-game.py | 88 ++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/guessing-game/guessing-game.py b/guessing-game/guessing-game.py index 65ba9c1..ffd2e2b 100644 --- a/guessing-game/guessing-game.py +++ b/guessing-game/guessing-game.py @@ -2,43 +2,51 @@ # Introduction print("Welcome to the Guessing Game!") -print("I'm thinking of a number between 1 and 100.") -print("You have 7 attempts to guess it correctly.") - -# Generate a random number between 1 and 100 -secret_number = random.randint(1, 100) - -# Set the number of allowed attempts -attempts = 7 - -# Game loop -while attempts > 0: - try: - # Ask the user for their guess - guess = int(input("Enter your guess: ")) - - # Ensure that guess is between 1-100 - if guess < 1 or guess > 100: - print("Please enter a number between 1 and 100.\n") - continue - - # Check if the guess is correct - if guess == secret_number: - print("🎉 Congratulations! You guessed the number!") - break - elif guess < secret_number: - print("Too low. Try a higher number.") - else: - print("Too high. Try a lower number.") - - # Decrease remaining attempts - attempts -= 1 - print(f"Attempts left: {attempts}\n") - - except ValueError: - # Handle non-integer input - print("Invalid input. Please enter a number.\n") - -# If no attempts are left -if attempts == 0: - print(f"❌ Game over! The number was {secret_number}. Better luck next time.") + +while True: + print("I'm thinking of a number between 1 and 100.") + print("You have 7 attempts to guess it correctly.") + + # Generate a random number between 1 and 100 + secret_number = random.randint(1, 100) + + # Set the number of allowed attempts + attempts = 7 + + # Game loop + while attempts > 0: + try: + # Ask the user for their guess + guess = int(input("Enter your guess: ")) + + # Ensure that guess is between 1-100 + if guess < 1 or guess > 100: + print("Please enter a number between 1 and 100.\n") + continue + + # Check if the guess is correct + if guess == secret_number: + print("🎉 Congratulations! You guessed the number!") + break + elif guess < secret_number: + print("Too low. Try a higher number.") + else: + print("Too high. Try a lower number.") + + # Decrease remaining attempts + attempts -= 1 + print(f"Attempts left: {attempts}\n") + + except ValueError: + # Handle non-integer input + print("Invalid input. Please enter a number.\n") + + # If no attempts are left + if attempts == 0: + print(f"❌ Game over! The number was {secret_number}. Better luck next time.") + + # Play again option + play_again = input("Do you want to play again? (y/n): ").strip().lower() + if play_again != "y": + print("Thanks for playing!") + break \ No newline at end of file