""" ------------------------------------------------- Project: Guess the number Standard: ? School: Tauranga Boys' College Author: Trey Simpson Date: 7.3.25 Python: 3.5 ------------------------------------------------- """ #Imports import random #Intro def get_player_name(): """Ask for the player's name and return it.""" return input("Enter your name: ").strip().title() def play_round(player_name): """Play a single round of the number guessing game.""" secret_number = random.randint(1, 1000) guesses_left = 10 print(f"\nAlright {player_name}, it's time to guess a number between 1 and 1000.") while guesses_left > 0: try: guess = int(input(f"You have {guesses_left} guesses left. Enter your guess: ")) if guess < 1 or guess > 1000: print("Out of bounds! Please guess a number between 1 and 1000.") continue if guess == secret_number: print(f"🎉 Congratulations, {player_name}! You guessed the number {secret_number} correctly!") return "player" guesses_left -= 1 if guess < secret_number: print("Too low! Try guessing higher.") else: print("Too high! Try guessing lower.") except ValueError: print("Invalid input! Please enter a number between 1 and 1000.") print(f"\nYou ran out of guesses! The correct number was {secret_number}.") return "computer" def main(): """Main function to handle game logic.""" player_name = get_player_name() rounds_to_win = 2 # Best of 3 (first to 2 wins) player_score = 0 computer_score = 0 total_rounds = 3 print(f"\nWelcome, {player_name}! Let's play a best-of-three number guessing game.") for round_number in range(1, total_rounds + 1): print(f"\n--- Round {round_number} ---") winner = play_round(player_name) if winner == "player": player_score += 1 else: computer_score += 1 print(f"\nCurrent Score: {player_name}: {player_score} | Computer: {computer_score}") # End game early if one wins best of 3 if player_score == rounds_to_win: print(f"\n {player_name} wins the game! Well played! ") return elif computer_score == rounds_to_win: print(f"\n The computer wins the game! Better luck next time, {player_name}.") return # If all 3 rounds are played, determine the final winner if player_score > computer_score: print(f"\n {player_name} wins the game with a score of {player_score}-{computer_score}! 🏆") else: print(f"\n The computer wins the game with a score of {computer_score}-{player_score}. Better luck next time, {player_name}!") if __name__ == "__main__": main()