fork(4) download
# nims.py
# Exercise 2.6 – The game of Nims/Stones
# http://o...content-available-to-author-only...t.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2011/assignments/MIT6_189IAP11_hw2.pdf
# Written for Python 3.2
# Glenn Richard
# July 3, 2013


def play_nims(pile, max_stones):
    '''
    An interactive two-person game; also known as Stones.
    @param pile: the number of stones in the pile to start
    @param max_stones: the maximum number of stones you can take on one turn
    '''

    ## Basic structure of program (feel free to alter as you please):

    #    while [pile is not empty]:
    #        while [player 1's answer is not valid]:
    #        [ask player 1]
    #            [execute player 1's move]
    #       
    #        while [player 2's answer is not valid]:
    #            [ask player 2]
    #            [execute player 2's move]
    #
    #    print "Game over"

    player = 1
    print(str(pile) + " stones, initally")
    while pile > 0:
        taken = 0
 
        while not 1 <= taken <= max_stones:
            try:
                inp = input("\nPlayer " + str(player) + " > Number of stones? (1 - " + str(max_stones) + "): ")
                print("\nInput was: " + str(inp))
                taken = int(inp)
            except:
                taken = 0
            
        pile -= taken
        print("\n" + str(taken) + " stones taken; " + str(pile) + " remaining")
            
        max_stones = min(pile, max_stones)
        # Switch players
        player = player % 2 + 1
    print("\nGame over")
    print("\nPlayer " + str(player % 2 + 1) + " wins!")

play_nims(100, 5)
Success #stdin #stdout 0.06s 9568KB
stdin
4
3
3
5
three
1
7
2
4
1
999999
5
4
2
3
3
y
4
2
2
4
0
3
3
1
5
2
4
5
1
1
5
gazillion to the gazillionth power
3
3
4
2
1
5
stdout
100 stones, initally

Player 1 > Number of stones? (1 - 5): 
Input was: 4

4 stones taken; 96 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 93 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 90 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 5

5 stones taken; 85 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: three

Player 1 > Number of stones? (1 - 5): 
Input was: 1

1 stones taken; 84 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 7

Player 2 > Number of stones? (1 - 5): 
Input was: 2

2 stones taken; 82 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 4

4 stones taken; 78 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 1

1 stones taken; 77 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 999999

Player 1 > Number of stones? (1 - 5): 
Input was: 5

5 stones taken; 72 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 4

4 stones taken; 68 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 2

2 stones taken; 66 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 63 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 60 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: y

Player 2 > Number of stones? (1 - 5): 
Input was: 4

4 stones taken; 56 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 2

2 stones taken; 54 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 2

2 stones taken; 52 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 4

4 stones taken; 48 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 0

Player 2 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 45 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 42 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 1

1 stones taken; 41 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 5

5 stones taken; 36 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 2

2 stones taken; 34 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 4

4 stones taken; 30 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 5

5 stones taken; 25 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 1

1 stones taken; 24 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 1

1 stones taken; 23 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 5

5 stones taken; 18 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: gazillion to the gazillionth power

Player 2 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 15 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 3

3 stones taken; 12 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 4

4 stones taken; 8 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 2

2 stones taken; 6 remaining

Player 2 > Number of stones? (1 - 5): 
Input was: 1

1 stones taken; 5 remaining

Player 1 > Number of stones? (1 - 5): 
Input was: 5

5 stones taken; 0 remaining

Game over

Player 1 wins!