[Tutor] The game of nim in python

Michael Selik michael.selik at gmail.com
Fri Apr 22 16:09:35 EDT 2016


What's the problem you're trying to solve? Did you get an error?

Here's a quick revision. There's probably a more elegant way, but this
seems to work.

    #/usr/bin/env python

    from __future__ import print_function
    import random

    try:
        input = raw_input
    except NameError:
        pass # already using Python 3


    player = input('Enter your name: ')

    straws = random.randint(10, 20)
    if straws % 4 == 1:
        straws += 1

    while straws:
        print("It's the Computer's turn. {} straws left.".format(straws))
        n = random.randint(1, min(3, straws))
        straws -= n
        print("The Computer removes {} straws.".format(n))

        if not straws:
            print("The Computer won!")
            break

        print("It's your turn, {}. There are {} straws
left.".format(player, straws))
        n = None
        while not n:
            print("You can remove between 1 and {} straws.".format(min(3,
straws)))
            try:
                n = int(input('How many straws do you want to remove? '))
                if n < 1 or n > min(3, straws):
                    n = None
            except ValueError:
                pass
        straws -= n
        print("You removed {} straws.".format(n))

        if not straws:
            print("You won!")
            break


On Fri, Apr 22, 2016 at 1:58 PM Henderson, Kevin (GE Aviation, US) <
KevinM.Henderson at ge.com> wrote:

> Python to Jython.
>
> Can you help me with a Jython code for the Nim game?
>
> Removing 1-4 sticks out of 13, last player who picks up the last stick wins
>
> Player 1 vs player2(Computer)
>
> player1=str(input("Enter your name. "))
> player2="Computer"
> howMany=0
> gameover=False
> strawsNumber=random.randint(10,20)
>
> if (strawsNumber%4)==1:
>     strawsNumber+=1
>
> def removingStrawsComputer():
>     removedNumber=random.randint(1,3)
>     global strawsNumber
>     while removedNumber>strawsNumber:
>         removedNumber=random.randint(1,3)
>     strawsNumber-=removedNumber
>     return strawsNumber
>
> def removingStrawsHuman():
>     global strawsNumber
>     strawsNumber-=howMany
>     return strawsNumber
>
> def humanLegalMove():
>     global howMany
>     legalMove=False
>     while not legalMove:
>         print("It's your turn, ",player1)
>         howMany=int(input("How many straws do you want to remove?(from 1
> to 3) "))
>         if  howMany>3 or howMany<1:
>             print("Enter a number between 1 and 3.")
>         else:
>             legalMove=True
>     while howMany>strawsNumber:
>         print("The entered number is greater than a number of straws
> remained.")
>         howMany=int(input("How many straws do you want to remove?"))
>     return howMany
>
> def checkWinner(player):
>     if strawsNumber==0:
>         print(player," wins.")
>         global gameover
>        gameover=True
>         return gameover
>
> def resetGameover():
>     global gameover
>     gameover=False
>     return gameover
>
> def game():
>     while gameover==False:
>         print("It's ",player2,"turn. The number of straws left:
> ",removingStrawsComputer())
>         checkWinner(player1)
>         if gameover==True:
>             break
>         humanLegalMove()
>         print("The number of straws left: ",removingStrawsHuman())
>         checkWinner(player2)
> game()
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list