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

Tell us what’s happening:

I don’t know how else I can add the result to the current value of board_str

Your code so far

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

# User Editable Region

    def __str__(self):
        board_str=''
        for row in self.board:
            row_str = " ".join([str(i) if i else '*' for i in row])
            board_str+=row_str 
    
    
    def find_empty_cell(self):

# User Editable Region

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

    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
        return False

def solve_sudoku(board):
    gameboard = Board(board)
    print(f'Puzzle to solve:\n{gameboard}')
    if gameboard.solver():
        print(f'Solved puzzle:\n{gameboard}')
    else:
        print('The provided puzzle is unsolvable.')
    return gameboard

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

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 75

Syntactically I believe this is correct, however it’s not what the test expects.

Next, join the items in row_str with a space and add the result to the current value of board_str.

Try to do this altogether in a new line, instead of modifying the existing line.

Thank you for your response:) however I’m not sure what you mean. I tried putting it into a new line, and came up with this:
board_str = ‘’.join(’ '.join(str(i) if i else ‘*’ for i in row) + ’ ’ for row in self.board).strip()

Bu the code still does not run

board_str += ’ '.join([str(i) if i else ‘*’ for i in row])

I also tried this

Don’t combine the join with the comprehension. Leave that alone and start a new line.

        for row in self.board:
            row_str = [str(i) if i else '*' for i in row]

This is the existing, leave it alone. The for loop is finished it’s work building the row_str list. Do this instruction on a new line:

Next, join the items in row_str with a space and add the result to the current value of board_str.

Okay, this is what I have so far:

for row in self.board:
    row_str = [str(i) if i else '*' for i in row]
    board_str+= '     '.join(str(i) if i else '*' for i in row)

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Ok, two things:

  1. You do not need to use a comprehension. You can look up how to use join here: https://www.w3schools.com/python/ref_string_join.asp
  2. Instruction says “a space” which likely means 1 space character, as you did originally. Not sure why you did 5 spaces this time?
  3. More of a style thing, but I would keep spaces around your “+=” operator and all operators generally. You can review this here: https://peps.python.org/pep-0008/#other-recommendations

Ok 3 things!