0

I'm trying to create a very simple tictactoe game.

I have this if-statement which dosen't seem to work as I want:

#Defining the board. 3 rows with 3 cells
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

#Player icons
p1_icon = "X"
p2_icon = "O"

#Player input
p1_input_row = 0
p1_input_cell = 0
p2_input_row = 0
p2_input_cell = 0
#Player input text
p1_turn = "Player 1's turn: "
p2_turn = "Player 2's turn: "

#String for invalid answer
invalid = "Invalid answer! Try again."

#Function for printing the board
def print_board():
    print("-------")
    print("|" + str(board[0][0]) + "|" + str(board[0][1]) + "|" + str(board[0][2]) + "|")
    print("-------")
    print("|" + str(board[1][0]) + "|" + str(board[1][1]) + "|" + str(board[1][2]) + "|")
    print("-------")
    print("|" + str(board[2][0]) + "|" + str(board[2][1]) + "|" + str(board[2][2]) + "|")
    print("-------")

#Main game-loop
while True:
    #Player 1's turn
    print(p1_turn)
    p1_input_row = int(input("Row: "))
    #Check player 1's input
    if p1_input_row == 1 or 2 or 3:
        p1_input_cell = int(input("Cell: "))
        if p1_input_cell == 1 or 2 or 3:
            board[(p1_input_row - 1)][(p1_input_cell - 1)] = p1_icon
            print_board()
        else:
            print(invalid)
    elif p1_input_row == 4:
        print("WTF")
    else:
        print(invalid)
    break

I want to check if the player (p1_input_row) choose a row between 1 and 3. If so, then the program should ask the player to choose a cell: p1_input_cell = int(input("Cell: ")). If the player didn't choose a row between 1 and 3, then display a error message. The same if-statement goes for p1_input_cell.

When I write 4for example, then the error message dosn't display. The program asks for the cell instead. Why is that?

Mr. Viggo
  • 69
  • 1
  • 10

1 Answers1

2

The test:

p1_input_row == 1 or 2 or 3

always returns True, because Python interprets this differently than you might expect:

All non-zero values in Python are interpreted as True. The or statement is comparing each of the entities against each other versus against p1_input_row

It interprets the code as:

p1_input_row == 1 or True or True 

You should rewrite your test as follows:

if p1_input_row == 1 or p1_input_row == 2 or p1_input_row == 3:

OR

if p1_input_row in [1, 2, 3]:
    # do something
E. Ducateme
  • 4,028
  • 2
  • 20
  • 30
  • The code: ``if p1_input_row in [1, 2, 3]:``: Do you ask for: If ``p1_input_row`` is ``in`` a new list ``[1, 2, 3]`` then perform an action? – Mr. Viggo Nov 01 '17 at 09:17
  • 1
    If you have a small collection of possible values, using the 'in' statement and comparing your known value to the values in the collection is a reasonable alternative instead of using multiple 'if' statements or using multiple 'or' statements. Here, based on your question there were only three possible values, so I put them in a list and used 'in' to test whether the value associated with p1_input_row was present in the list. – E. Ducateme Nov 01 '17 at 09:36
  • Okay, thanks for the answer! – Mr. Viggo Nov 01 '17 at 11:13