-3

Here is a solve function which solves a sudoku puzzle:

def solve(grid):
    for y in range(9):
        for x in range(9):
            if grid[y][x]==0:
                for n in range(1,10):
                    if possible(grid,y,x,n):
                        grid[y][x]=n
                        solve(grid)
                        grid[y][x]=0
                return
    print(np.matrix(grid))
    return grid

When solve function is called, it takes a matrix 9*9 sudoku puzzle and prints the solved puzzle.

The function works properly and the puzzle gets printed the right way. But instead of printing the puzzle I want the function to return the grid matrix and return in a variable like this:

solved_grid=solve(grid)

Instead of returning the grid the solve function returns None.

Kindly advise. Thanks in advance

guidot
  • 5,095
  • 2
  • 25
  • 37
  • 5
    You return `None` if `grid[y][x]==0`, which is in the first iteration of the first two `for` loops. – Guy Mar 11 '20 at 09:50
  • 1
    ^--- This and shouldn't your recursive call be `grid = solve(grid)` ? – Cid Mar 11 '20 at 09:51
  • Does this answer your question? [I expect 'True' but get 'None'](https://stackoverflow.com/questions/15210646/i-expect-true-but-get-none) – guidot Mar 11 '20 at 10:11

1 Answers1

0

That is because after your third loop you have a return,which does not return anything,but also terminates the execution of the program.