From 5505bf97225158ffcdc5995f75c4dfb9893e7031 Mon Sep 17 00:00:00 2001 From: TomerDahari Date: Mon, 14 Oct 2024 11:34:56 +0300 Subject: [PATCH] I added the 'guess the number' game --- guess_the_number/game.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 guess_the_number/game.py diff --git a/guess_the_number/game.py b/guess_the_number/game.py new file mode 100644 index 00000000..b0fdc582 --- /dev/null +++ b/guess_the_number/game.py @@ -0,0 +1,37 @@ +import random + +def guess_game(): + print("Welcome to 'Guess the Number' game!") + + # Setting the maximum number of attempts + max_attempts = 5 + attempts = 0 + + # The computer chooses a random number between 1 and 100 + number_to_guess = random.randint(1, 100) + + # The guessing game + while attempts < max_attempts: + try: + # A request from the player to guess a number + guess = int(input(f"Attempt {attempts + 1}/{max_attempts}: Enter a number between 1 and 100: ")) + + # Checking whether the guess is close to the selected number + if guess < number_to_guess: + print("Too low!") + elif guess > number_to_guess: + print("Too high!") + else: + print(f"Congratulations! You guessed the number {number_to_guess} correctly!") + break + + attempts += 1 + + except ValueError: + print("Invalid input! Please enter a valid number.") + + if attempts == max_attempts and guess != number_to_guess: + print(f"Sorry, you've used all {max_attempts} attempts. The correct number was {number_to_guess}.") + +if __name__ == "__main__": + guess_game()