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

Tell us what’s happening:

Not sure where my mistake is in my for loop. And when I submit my code I am prompted that my for loop should use range( row_start, row_start + 3) and I am not sure where they are getting that from.

Your code so far

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))

# User Editable Region

    def valid_in_square(self, row, col, num):
        row_start = (row // 3) * 3
        col_start = (col // 3) * 3
        for row_no in range(row_start):
            pass

# User Editable Region

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)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36

Challenge Information:

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

The sequence should start at row_start, which is the first row of the 3x3 square.
range(row_start) would start from zero and end a cell before row_start.

I’m not sure if that clarifies your doubt.

1 Like

Yes that definitely helped me.

range(row_start, row_start + 3)

I need a simpler explanation on my we need the row_start + 3

Actually I figured it out. the variable + 3 is to iterate over 3 variables correst?

Let’s say you want to check the central 3x3 square. You need to iterate on rows and columns to do that.

The starting row is row_start. If you use range(row_start, row_start + 3), the last iteration will be at row_start + 2 because row_start + 3 is not included.

The same is true for columns.