import random STARTING_CHIPS = 21 def setup(): player1_name = input("Enter Player 1's name: ") player2_name = input("Enter Player 2's name: ") rounds = int(input("Enter the number of rounds: ")) return player1_name, player2_name, rounds def maintenance(): chips = STARTING_CHIPS p1_score = 0 p2_score = 0 currently_playing = random.choice(["Player 1", "Player 2"]) return chips, p1_score, p2_score, currently_playing def game_loop(player1_name, player2_name, rounds): chips, p1_score, p2_score, currently_playing = maintenance() for round_number in range(1, rounds + 1): print(f"\nRound {round_number}") while chips > 0: print(f"\nChips left: {chips}") print(f"It's {currently_playing}'s turn.") try: chips_taken = int(input("How many chips do you want to take? (1-3): ")) if chips_taken < 1 or chips_taken > 3: print("That's not a valid number of chips! Please choose 1, 2, or 3.") continue chips -= chips_taken if chips <= 0: if currently_playing == "Player 1": p1_score += 1 print(f"{player1_name} wins this round!") else: p2_score += 1 print(f"{player2_name} wins this round!") break else: currently_playing = "Player 2" if currently_playing == "Player 1" else "Player 1" except ValueError: print("That's not a valid input! Please enter a number.") return p1_score, p2_score def final_winner(player1_score, player2_score, player1_name, player2_name): if player1_score > player2_score: print(f"\n{player1_name} wins the game with a score of {player1_score}!") elif player2_score > player1_score: print(f"\n{player2_name} wins the game with a score of {player2_score}!") else: print("\nIt's a tie!") def main(): print("Welcome to the 21 Chips Game!") player1_name, player2_name, rounds = setup() player1_score, player2_score = game_loop(player1_name, player2_name, rounds) final_winner(player1_score, player2_score, player1_name, player2_name) if __name__ == "__main__": main()