
# playNim.py


def getPileIdx(currPlayer):
  while True:
    idx = int(input("Player " + str(currPlayer) + ": pile (1-" + str(len(piles)) + "): ")) - 1
    if (idx >= 0) and (idx < len(piles)) and (piles[idx] > 0):
      return idx
    else:
      print("Invalid pile! Try again.")


def getChips(currPlayer, pileIdx):
  while True:
    n = int(input("Player " + str(currPlayer) + ": no. of chips: "))
    if n > 0 and n <= piles[pileIdx]:
      return n
    else:
      print("Invalid number! Try again.")



piles = [3, 4, 5]
isPlayerA = True

while True:
  currPlayer = "A" if isPlayerA else "B"

  print("Piles:", piles)
  if sum(piles) == 0:
    print("Player", currPlayer, "loses")
    break

  pileIdx = getPileIdx(currPlayer)
  nChips = getChips(currPlayer, pileIdx)

  piles[pileIdx] -= nChips
  isPlayerA = not isPlayerA
