Learn Classes and Objects by Building a Sudoku Solver - Step 33

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

    def find_empty_cell(self):
        for row, contents in enumerate(self.board):
            try:
                col = contents.index(0)
                return row, col
            except ValueError:
                pass
        return None

    def valid_in_row(self, row, num):
        return num not in self.board[row]

    def valid_in_col(self, col, num):
        return all(self.board[row][col] != num for row in range(9))

puzzle = [
  [0, 0, 2, 0, 0, 8, 0, 0, 0],
  [0, 0, 0, 0, 0, 3, 7, 6, 2],
  [4, 3, 0, 0, 0, 0, 8, 0, 0],
  [0, 5, 0, 0, 3, 0, 0, 9, 0],
  [0, 4, 0, 0, 0, 0, 0, 2, 6],
  [0, 0, 0, 4, 6, 7, 0, 0, 0],
  [0, 8, 6, 7, 0, 4, 0, 0, 0],
  [0, 0, 0, 5, 1, 9, 0, 0, 8],
  [1, 7, 0, 0, 0, 6, 0, 0, 5]
]

gameboard = Board(puzzle)
valid_in_col(gameboard,0,7)

Step 33

Call valid_in_col on gameboard. Pass it 0 and 7 as the arguments to see if the number 7 is allowed in the first column of the board and print the result.

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

Hello,

Thank you for replying.

I am trying to call the function I have defined as “valid_in_col” on the class “gameboard” to check if the number 7 is valid in the provided column or not.

Regards,

Okay, and what about that has you stuck?

I thought I called the function on the class as requested when I put “valid_in_col(gameboard,0,7)” but it is still giving me an error. What am I missing ?

This is the error message it gave me
" Sorry, your code does not pass. Keep trying.

You should print the result of calling valid_in_col(0, 7) on gameboard."

Also tried passing it through the print option by putting “print(valid_in_col(gameboard,0,7))” and it still gave me an error.

Link to the step:
https://www.freecodecamp.org/learn/scientific-computing-with-python/learn-classes-and-objects-by-building-a-sudoku-solver/step-33

Keep in mind gameboard is a class object, so the method is part of that class. The syntax to access a method of a class is this:

classobject.method(arguments)

You can also review steps 27 - 29 which is the same:
https://www.freecodecamp.org/learn/scientific-computing-with-python/learn-classes-and-objects-by-building-a-sudoku-solver/step-27

Thank you so much . That worked!!!. Thought about it wrong

1 Like