-1

This program is for a tic tac toe game and I designed a function to check for when either X or O would win. The if statement is practically the same just for different lines but every time I put in a certain position in the board it will print "X wins" despite not having all three X's, which makes me think the and operator isn't working.

Does anybody have any input as to what could be happening?

if ("X" in (board[0][0] and board[2][0] and board[4][0])) or ("X" in (board[0][0] and board[0][2] and board[0][4])):
    print("X wins")

board = [[" ", "|" ," ", "|" ," "],
         ["-", "-", "-", "-", "-"],
         [" ", "|" ," ", "|" ," "],
         ["-", "-", "-", "-", "-"],
         [" ", "|" ," ", "|" ," "],
         ["-", "-", "-", "-", "-"]]
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

1

I believe the problem lies in your parentheses. Specifically:

(board[0][0] and board[2][0] and board[4][0])

will evaluate to a string in your case. And this program will test if "X" in (string). I think what you want to test is if "X" is in all 3 of these spaces. You could do it like this:

if ("X" in board[0][0] and "X" in board[2][0] and "X" in board[4][0])

Just one example of what you could do, but this should help you out!

martineau
  • 119,623
  • 25
  • 170
  • 301
Dennis
  • 11
  • 3