#--------------------------------------------------- # name: 21 # author: Cody McQueen # purpose: 21 game # created: #---------------------------------------------------- import random def play_21(): print("Welcome to the game of 21") player1_name = input("Enter Player 1's name: ") player2_name = input("Enter Player 2's name: ") num_rounds = int(input("How many rounds would you like to play? ")) player1_score = 0 player2_score = 0 for round_num in range(1, num_rounds + 1): print("\nRound", round_num) num_chips = random.randint(10, 20) # Randomly choose initial number of chips print("Initial number of chips:", num_chips) current_player = player1_name while num_chips > 0: print("\n{}'s turn. Pick how many?".format(current_player)) chips_taken = int(input("Enter 1, 2, or 3: ")) while chips_taken < 1 or chips_taken > 3 or chips_taken > num_chips: print("Invalid input. Please enter a number between 1 and 3 and less than the remaining chips.") chips_taken = int(input("Enter 1, 2, or 3: ")) num_chips -= chips_taken print("Number of chips remaining:", num_chips) if num_chips <= 0: print("\n{} wins this round!".format(current_player)) if current_player == player1_name: player1_score += 1 else: player2_score += 1 break # Switch players current_player = player1_name if current_player == player2_name else player2_name print("\nGame over!") print("Final scores:") print("{}: {}".format(player1_name, player1_score)) print("{}: {}".format(player2_name, player2_score)) if player1_score > player2_score: print("{} wins!".format(player1_name)) elif player2_score > player1_score: print("{} wins!".format(player2_name)) else: print("It's a tie!") play_21()