Due: February 22nd, 2017, before midnight
Late penalties: Thu -5%, Fri -10%, Sat, Sun 20%.
nim.py
, and run
it. Try taking stones, until there are none left.
stones_taken
from the
user, check: if it's more than pile_size
,
print 'Too many! There are only', pile_size, 'stones' continueThe statement
continue
will skip the rest of
the actions in the loop, and go back to the top of
the while
loop.
print 'You lose!' break
player_ID = 1and, just after
while True:
,
print 'Player', player_ID, 'moves next'At the bottom, change
player_ID
: if it is 1,
make it 2, and vice versa.
Finally, change 'You lose!' to something that prints one of these two :
'Player 1 loses!' 'Player 2 loses!'depending on who just took the last stone
print '-' * 20before the line
print 'Player', player_ID, 'moves next'
pile_size =
5
with three initializations:
pile1_size = 5 pile2_size = 5 pile3_size = 5Now, the line
print 'Pile has', pile_size,
'stones'
has to be replaced with three print
statements.
Also, you have to ask the user which pile to take from:
pile_number = int(raw_input('Which pile to take from? '))Also, you have to check if the pile chosen has enough stones, so you will need to change the input validation:
if (pile_ID == 1 and stones_taken > pile1_size) or (pile_ID == 1 and ...)Another consequence: The game is over when all three piles are empty. So, modify the code that checks if some user has lost.
nim.py
. Download the other
file nim_list.py
.
I have written some of the code for you.
CAREFUL! The user will type a pile_ID
from 1 to 3, but the list indexes go from 0 to 2. So,
always write pile_sizes[pile_ID - 1]
to
compensate.