""" ------------------------------------------------- Project: Take-Away Game Standard: 91883 (AS1.7) v.1 School: Tauranga Boys' College Author: Malove Prajapati Date: 22/03/2024 Python: 3.5 ------------------------------------------------- """ def main(): print("Welcome to the Take-Away game!") #asks for player names player1 = input("Enter the first player's name: ") player2 = input("Enter the second player's name: ") print("This is the 21 Game, during each turn you have to subtract 1-3 chips from the total amount of chips, which is 21.") rounds = int(input("How many rounds would you like to play? ")) #player score counters player_scores = {player1: 0, player2: 0} #loop for the amount of rounds that the player has selected for round_number in range(1, rounds + 1): print(f"\nRound {round_number} starts now!") MAX_CHIPS = 21 print(f"The current amount of chips are {MAX_CHIPS} chips.") #gives the first registered player the first turn of the round current_player = player1 while MAX_CHIPS> 0: print(f"\n{current_player}'s turn. You can take 1, 2, or 3 chips.") #this loop prevents any strings from being entered into the code and prompts the user to enter a number from the choices before while True: try: chips_taken = int(input("Chips taken: ")) except ValueError: print("Invalid input. Please enter a valid integer.") continue #prevents them from taking 0 chips or more than 3 or more than 21 chips if chips_taken < 1 or chips_taken > 3 or chips_taken > MAX_CHIPS: print("Invalid input. Please pick 1, 2, or 3 chips.") continue break #takes away the amount of chips the player has selected from the original amount of chips MAX_CHIPS -= chips_taken print(f"{MAX_CHIPS} chips remaining.") if MAX_CHIPS == 0: print(f"\n{current_player} wins this round!") player_scores[current_player] += 1 break current_player = player2 if current_player == player1 else player1 #prints the final scores after all rounds have ended print("\nGame over!") print("Final scores:") for player, score in player_scores.items(): print(f"{player}: {score} point(s)") #prints the winnning players/scores of the game if player_scores[player1] > player_scores[player2]: print(f"\n{player1} wins this game!") elif player_scores[player2] > player_scores[player1]: print(f"\n{player2} wins this game!") else: print("\nYou tied!") if __name__ == "__main__": main()