Answer:
Explanation:
The following code makes all the necessary changes so that the game runs as requested in the question. The only change that was not made was making the number of coins random since that contradicts the first rule of the game that states that the game starts with 22 coins.
turn = 0
remaining_coins = 22
print("Take turns removing 1, 2, or 3 coins. ")
print("You win if you take the last coin.")
while remaining_coins > 0:
print("\nThere are", remaining_coins, "coins remaining.")
if turn % 2 == 0:
# Player1
taken_coins = int(input("Player 1: How many coins do you take?"))
turn += 1
else:
# Player2
taken_coins = int(input("Player 2: How many coins do you take?"))
turn += 1
if taken_coins < 1 or taken_coins > 3 or taken_coins > remaining_coins:
print("That's not a legal move. Try again. ")
print("\nThere are", remaining_coins, "coins remaining.")
if turn % 2 == 0:
# Player1
taken_coins = int(input("Player 1: How many coins do you take? "))
turn += 1
else:
# Player2
taken_coins = int(input("Player 2: How many coins do you take? "))
turn += 1
remaining_coins -= taken_coins
else:
remaining_coins -= taken_coins
if remaining_coins - taken_coins == 0:
print("No more coins left!")
if turn % 2 == 0:
print("Player 1 wins!")
print("Player 2 loses !")
else:
print("Player 2 wins!")
print("Player 1 loses!")
remaining_coins = remaining_coins - taken_coins
else:
continue