Skip to content

PYTHON TypeError: validTicTacToe() missing 1 required positional argument: ‘board’

I am new to python and trying to run this code on VScode. It gives an error saying TypeError: validTicTacToe() missing 1 required positional argument: ‘board’. What am I doing wrong here? I am actually trying to understand how does the self works. I know c++ so if you can explain in comparison to c++ it would be of great help.

Also, this is a leetcode problem, it works good on leetcode, but looks like I have to make some changes before running it on VScode. TIA

def validTicTacToe(self, board) -> bool:
    X, O = self.countSymbols(board)
    if O > X:
        # Os can't  be greater than Xs
        return False
    elif abs(X-O) > 1:
        # Difference can only be 1
        return False
    elif X > O:
        # X can't have more moves than O if O is already won
        if self.checkTriads(board, 'O'): return False
        # X and O can't have equal moves if X is winning
        if self.checkTriads(board, 'X'): return False
    return True

def countSymbols(self, board):
    X = 0
    O = 0
    for row in board:
        for i in row:
            if i == 'X':
            elif i == 'O':
    return X,O
def checkTriads(self, board, sym='X'):
    # Checking for Hight triads
    i = 0
    while i<3:
        if (board[0][i] == board[1][i] == board[2][i] == sym):
            return True
    # Checking for width
    while i<3:
        if (board[i][0] == board[i][1] == board[i][2] == sym):
            return True
    # Checking for diag.
    if (board[0][0] == board[1][1] == board[2][2] == sym):
            return True
    if (board[0][2] == board[1][1] == board[2][0] == sym):
            return True
    return False

board=["O  ","   ","   "]



You defined your functions in the manner of object methods. These will always automatically receive their instance object as the first parameter. By convention, this parameter is named self. You however didn’t declare a class to contain these methods. You can either remove the self parameters from your functions or wrap them inside a class definition:

class TicTacToe():
    def validTicTacToe(self, board) -> bool:
        # ...

game = TicTacToe()
board = ["O  ", "   ", "   "]

To make actual use of the class mechanism, the board should be a property:

class TicTacToe():
    def __init__(self, board):
        self.board = board

    def validTicTacToe(self) -> bool:
        # now you can use self.board instead of passing it around

board = ["O  ", "   ", "   "]
game = TicTacToe(board)
3 People found this is helpful