import random def guess_number(): # Asks the player for their name player_name = input("Please enter your name: ") # Set Player and Gary wins counters player_wins = 0 computer_wins = 0 # Main game loop, continues until either player or Gary wins 2 rounds while player_wins < 2 and computer_wins < 2: # Generate a random number between 1 and 100 for each game secret_number = random.randint(1, 100) attempts = 10 print("\nNew Game: Hello, I am Gary and welcome to my number guessing game, " + player_name + "!") print("I've picked a number between 1 and 100. You have 10 attempts to guess it.") # Guessing loop for each game, gives player 10 attempts to guess the number while attempts > 0: guess = int(input("Enter your guess: ")) if guess == secret_number: print("Congratulations, " + player_name + "! You guessed the number correctly!") player_wins += 1 print(player_name + " wins:", player_wins, "Gary wins:", computer_wins) break elif guess < secret_number: print("The number is higher.") else: print("The number is lower.") attempts -= 1 print("Attempts left:", attempts) # If player runs out of attempts, Gary wins the round if attempts == 0: print("Sorry, " + player_name + ", you've run out of attempts. The number was:", secret_number) computer_wins += 1 print(player_name + " wins:", player_wins, "Gary wins:", computer_wins) # After the loop ends, declare the winner of the match based on the number of wins if player_wins >= 2: print("\nCongratulations, " + player_name + "! You have defeated me!") else: print("\nSorry, " + player_name + ". Gary wins, better luck next time!") # Run the game guess_number()