""" ------------------------------------------------- Project: Guess The Number Game Standard: 91883 (AS1.7) v.1 School: Tauranga Boys' College Author: YOUR FULL NAME Date: THE DATE YOU COMPLETED THE CODE Python: 3.5 ------------------------------------------------- """ import random # Function to start a new round of guessing def play_round(player_name): # Initialize variables number_to_guess = random.randint(1, 1000) # Random number between 1 and 1000 attempts_left = 10 # Player has 10 attempts guessed_correctly = False # Flag to track if the player has guessed correctly print(f"\nHello {player_name}, it's time to guess a number between 1 and 1000!") # The guessing loop, where the player tries to guess the number while attempts_left > 0: print(f"\nYou have {attempts_left} attempts left.") guess = int(input("Enter your guess: ")) # Check if the guess is correct if guess == number_to_guess: guessed_correctly = True break # Exit the loop if the guess is correct elif guess < number_to_guess: print("Your guess is too low! Try again.") else: print("Your guess is too high! Try again.") # Decrease the number of attempts attempts_left -= 1 return guessed_correctly # Main function to run the game def play_game(): # Ask for the player's name player_name = input("Please enter your name: ") # Initialize scores player_score = 0 computer_score = 0 rounds_played = 0 # Game loop for 3 rounds or until a player wins 2 rounds for round_num in range(1, 4): print(f"\nRound {round_num}") print("-" * 20) # Start a new round and check if the player guessed correctly if play_round(player_name): player_score += 1 # Player scores if they guess correctly print(f"Congratulations {player_name}! You guessed the number correctly.") else: computer_score += 1 # Computer scores if the player runs out of attempts print(f"Sorry {player_name}, you lost this round. The number was {random.randint(1, 1000)}.") # Update rounds played rounds_played += 1 # If a player wins two rounds, the game ends early if player_score == 2 or computer_score == 2: break # Final game feedback print("\nGame Over!") print(f"Total Rounds Played: {rounds_played}") print(f"{player_name}'s Score: {player_score}") print(f"Computer's Score: {computer_score}") if player_score > computer_score: print(f"Congratulations {player_name}, you won the game!") elif player_score < computer_score: print("The computer won the game. Better luck next time!") else: print("It's a draw!") # Run the game if __name__ == "__main__": play_game()