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

Tell us what’s happening:

I know what I am doing is wrong.
This is the instructions.
“Outside the Board class, after initializing the gameboard object, use gameboard.board to access the board attribute of your gameboard object and print the result to the screen.”

Your code so far


# User Editable Region

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

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)

# User Editable Region

Your browser information:

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

Challenge Information:

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

This is key. You’re trying to print inside the Board class. Where do you initialise the gameboard object?

class Board:
def init(self, board):
self.board = board
self.gameboard = board
print (gameboard.board)

I have tried this code. It is showing “NameError: name ‘gameboard’ is not defined”

gameboard is not defined because you are trying to access it before it is initialised.

As I said, you shouldn’t doing anything inside class Board.
Hit the Reset button to restore the starting code for this step and then add your line of code after the line where you initialise the gameboard object.

I am sorry. I didnt understand.

Reset the lesson.
Now find gameboard in your code.
Put your line of code below that.

You can’t use a variable before it’s declared.

Have to declare it to an empty string?

gameboard is already declared, right after the declaration of puzzle.
So, gameboard creates (instantiates) an instance of the Board class.

This line of code which you created previously…

…will print the board that was just created with this line of code:

gameboard = Board(puzzle)

You can’t put your code anywhere before gameboard is created, so you must put it after it.

Oh… I got it. Thanks a lot

1 Like