0

below is code that I've written to check whether or not a Sudoku board (referenced here as board) has a solution or not.

Whenever I run the function solve_board, both the board_copy and board variables change. The board variable will match the board_copy variable. I expect board_copy to change, since variables in Python are passed by assignment. Why does board change? It's not passed in to the function so I wouldn't expect the function to impact it.

board_copy = board                
solve_board(board_copy)

def solve_board(board):
    for row in range(9):
        for col in range(9):
            if board[row][col] == 0:
                for num in range(1, 10):
                    if valid_move(row, col, num, board):
                        board[row][col] = num
                        if solve_board(board, "possible"):
                            return True
                        board[row][col] = 0
                return False 
    return True
  • `board_copy` is *not* a copy. You have two references to the same object. See https://nedbatchelder.com/text/names.html – chepner Sep 06 '20 at 14:05

1 Answers1

0

The line:

board_copy = board

States that board_copy points on board so if you change either of the other changes too.

They are different names pointing on the same object.

Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22