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

Tell us what’s happening:

What do they mean by current cell? I have the self.solver on the last line like it says.

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

    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, row_start + 3):
            for col_no in range(col_start, col_start + 3):
                if self.board[row_no][col_no] == num:
                    return False
        return True

    def is_valid(self, empty, num):
        row, col = empty
        valid_in_row = self.valid_in_row(row, num)
        valid_in_col = self.valid_in_col(col, num)
        valid_in_square = self.valid_in_square(row, col, num)
        return all([valid_in_row, valid_in_col, valid_in_square])

# User Editable Region

    def solver(self):
        if (next_empty := self.find_empty_cell()) is None:
            return True
        for guess in range(1, 10):
            if self.is_valid(next_empty, guess):
                row, col = next_empty
                self.board[row][col] = 0
                if self.solver():
                    return True

# 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/128.0.0.0 Safari/537.36

Challenge Information:

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

Is your statement after the innermost if?

Which one are you referring to?

Seen 5 other posts about this step what is the reason its so difficult to explain better?

The innermost if statement will be the most indented. This means it’s inside all of the other ifs and there’s no more if statements contained within it.

So I tried putting the 0 in the innermost if statement in the parenthesis and after the colon below, still not passing.

def solver(self):
        if (next_empty := self.find_empty_cell()) is None:
            return True
        for guess in range(1, 10):
            if self.is_valid(next_empty, guess):
                row, col = next_empty
                self.board[row][col] = guess
            if self.solver():0
            return True

this is not correct syntax, what do you want it to do?

This is the error message I keep getting " You should set the current cell value back to 0 after the innermost if statement."

you need to figure out two things there

  1. set the current cell value back to 0
  2. after the innermost if statement

so a what and a where.

so let’s start with the where. You are correct that the if statement with if self.solver(): is the innermost statement, but you should not touch it, you should add new code after (and outside) this if statement

So when your referring to outside what does that mean? Looking at the code from the current line of code or outside referring to a different line of code within the definition?

not inside the if statement, it needs to go after it and indented in a way that is not part of the if statement

python uses indentation to indicate what is part of a specific block

if xxx:
  #this
  #is
  #all
  #inside
  #the
  #if
  #statement
#this is outside the if statement
#look at the indentation

So this is outside the if statement correct?

def solver(self):
        if (next_empty := self.find_empty_cell()) is None:
            return True
        for guess in range(1, 10):
            if self.is_valid(next_empty, guess):
                row, col = next_empty
                self.board[row][col] = guess
            if self.solver(0): 
                return True
                pass

How do I make the code outside the if statement do I put it with the return statement after the If condition or write a completely new line of code and include the 0?

How do you make code to be within the if statement?

The if statement starts indented 1 to 4 spaces after the last line of code.

This is my latest guess as to what its asking based on what I saw from other comments. Don’t get why though and the reason for the extra line of code.

def solver(self):
        if (next_empty := self.find_empty_cell()) is None:
            return True
        for guess in range(1, 10):
            if self.is_valid(next_empty, guess):
                row, col = next_empty
                self.board[row][col] = guess
                if self.solver(): 
                    return True
                    self.board[row][col] = 0

And so how would you make some code not part of the if statement. (You would do the opposite of what you’ve said here)

If you have code after the return statement like this, it will never run. return will exit the function.

Did what you said and still not passing.

def solver(self):
        if (next_empty := self.find_empty_cell()) is None:
            return True
        for guess in range(1, 10):
            if self.is_valid(next_empty, guess):
                row, col = next_empty
                self.board[row][col] = guess
            if self.solver():
            self.board[row][col] = 0
            return True

you did not do what was said.
why is the if statement empty now?

I don’t understand what you mean by empty. I also moved the self.board[row][col] = 0 before the return True statement?