basic 2 player wordgame

Baba raoulbia at gmail.com
Tue Sep 21 16:35:09 EDT 2010


On Sep 21, 1:39 pm, Mel <mwil... at the-wire.com> wrote:
> Baba wrote:
> > I am working on a simple wordgame exercise: 2 players form a word by
> > alternating turns saying a letter, which is added on to the end of the
> > word fragment.
>
> > I am familiar with loops, iterations etc but i need a hint as to how
> > to approach alternating turns when writing this code?
>
> One way (not tested):
>
> thisplayer = Player()
> otherplayer = Player()
> while not_won:
>     thisplayer.take_turn()
>     thisplayer, otherplayer = otherplayer, thisplayer
>
>         Mel.


Hi Mel,

Thank you very much. Your suggestion works like a charm :)

def alternating_turns():
    hand = []
    thisPlayer = 'player1'
    otherPlayer = 'player2'
    while len(hand) < 3:
        print 'turn %s: ' %(thisPlayer)
        letter = raw_input('enter letter: ')
        hand.append(letter)
        thisPlayer, otherPlayer = otherPlayer, thisPlayer
    print hand

alternating_turns()

thanks again! much appreciated. this was a first for me where i have
learned a practical way to apply mutation.

Baba



More information about the Python-list mailing list