If Statement Error (Tic Tac Toe)

Mike Meyer mwm at mired.org
Tue Nov 1 21:36:47 EST 2005


ale.of.ginger at gmail.com writes:

> The code's indentation was fine - I forgot to declare cell in player
> two's section and not just in player one.
>
> The code (including the win check, for once!) is working.  The last
> obstacle is my tie checker; it doesn't seem to like what I have:
>
> if ((gameboard[0:9] is 'X' or 'O') and (win == 0)):
>      print "Tie."
>      win = 2
>
> Will the [0:9] range not work in this?  Should I write out each option?
>  Or is there some way to check if all things in the list are NOT ' '?
>
> I tried
>
> if ((gameboard[0:9] is not ' '))

Yes, those tests are not doing what you want them to do. On the other
hand, you can still test this in one swell foop with "in":

      if ' ' not in gameboard and win == 0:
         print "Tie."
         win = 2

That does assume that gameboard and gameboard[0:9] are the same
thing. But they should be. For that matter, the defaults for the slice
should be 0 and 9, so that gameboard[:] is the same thing yet
again. But since you're not changing the board but reading it, there's
no need to make a copy, which is what the sliced versions do.

    <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list