here's the code and i have ran out of ideas. i've tried changing the indenting, but it didn't work either. the purpose of the nim game is: "a player sets up 2 piles of blocks, the second player chooses whether to go first or second, they each take turns removing blocks (in this case instead of blocks i put -) the players have to remove at least one or not more than 3 blocks from any pile, whichever player takes the last block wins.
from math import *
from random import randint
pile1 = randint(1,20)
pile2 = randint(1,20)
player= 1
print("it's,"+str(player ) +' playing')
while pile1+pile2>0 :
if pile1>0:
print ('pile 1 :'+'-'*pile1)
if pile2>0:
print('pile 2:'+'-'*pile2)
break
which_pile=int(input('On which pile would you like to play ? : '))
while which_pile !=1 and which_pile != 2:
which_pile=int(input('On which pile would you like to play ? : '))
tokens=int(input('How many tokens would you like to take ? : '))
if which_pile==1:
while tokens<1 or tokens>min(3,pile1):
tokens=int(input('How many tokens would you like to take ? : '))
pile1-=tokens
else:
while tokens<1 or tokens>min(3,pile2):
tokens=int(input('How many tokens would you like to take ? : '))
pile2-=tokens
As formatted your code has no capability to ask for player 2.
It will
You are missing an outer loop that changes the player and continues until the games end condition (no more tiles left in piles) is met.
You also need some more safeguards in place to avoid ValueError
if no integers are inputted and a general cleanup to heed The style guide for python.
A completed program could look like this:
from math import *
from random import randint
pile1 = randint(1,20)
pile2 = randint(1,20)
player = "1"
while True:
print(f"\n\nPlayer {player}'s turn:")
print (f" pile 1: {'-' * pile1}")
print (f" pile 2: {'-' * pile2}\n")
if pile1 == 0 or pile2 == 0:
print(" Only one pile left.")
which_pile = "2" if pile1 == 0 else "1"
else:
# use strings to compare, no need to care about ValueErrors by int()
which_pile = input(' On which pile would you like to play? ')
while which_pile not in ("1", "2"):
which_pile = input(' On which pile would you like to play? ')
available_tokens = pile1 if which_pile == "1" else pile2
tokens = input(' How many tokens would you like to take ? ')
# make sure you do not take more then 3 (or the amount in the pile)
while tokens not in map(str, range(1, min(4, available_tokens+1))):
tokens = input(' How many tokens would you like to take ? ')
# only playe you really need an int - and its ensured above it
# can be converted to an integer
tokens = int(tokens)
if which_pile == "1":
pile1 -= tokens
else:
pile2 -= tokens
if pile1 + pile2 == 0:
break # exit while True loop
# change player
player = "2" if player == "1" else "1"
print(f"Player {player} wins" )
And a possible game would output like that:
Player 1's turn:
pile 1: -----
pile 2: --
On which pile would you like to play? 1
How many tokens would you like to take ? 3
Player 2's turn:
pile 1: --
pile 2: --
On which pile would you like to play? 1
How many tokens would you like to take ? 2
Player 1's turn:
pile 1:
pile 2: --
Only one pile left.
How many tokens would you like to take ? 1
Player 2's turn:
pile 1:
pile 2: -
Only one pile left.
How many tokens would you like to take ? 1
Player 2 wins
I use map(int, range(..)) to create the allowed strings to check the input agains.
I use ternary conditions for checks, see Does Python have a ternary conditional operator?