0

I'm relatively new to python and the concept of recursion. The following code has been taken from a computerphile video (link below). I'd like to understand why the solve function returns a none after the desired output of the solved sudoko, could someone clarify? Is there anyway I can tweak the code to remove it? Thanks.

https://youtu.be/G_UYXzGuqvM

def solve(sudoko):
    for i in range(9):
        for j in range(9):
            if sudoko.board[i][j] == 0:
                for m in range(1, 10):
                    if sudoko.possible(i, j, m):
                        sudoko.board[i][j] = m
                        solve(sudoko)
                        sudoko.board[i][j] = 0
                return
    print(sudoko.show())
  • if you don't put anything after the return keyword, you don't return anything, and can't get anything else than none. Maybe you want to `return sudoku` ? – B. Go Apr 06 '20 at 21:27
  • 1
    What are you expecting it to return? You typed `return` with no actual thing to return. Blank `return` or no `return` returns `None` by default. – ShadowRanger Apr 06 '20 at 21:28
  • The function doesn't return anything, it just updates the `sudoku` board in place, and then prints the board. – Barmar Apr 06 '20 at 21:29
  • Since the function doesn't return anything, you shouldn't try to print the return value, and then you won't see `None`. – Barmar Apr 06 '20 at 21:32
  • Related: [Why does my recursive function return None?](https://stackoverflow.com/q/17778372/4518341) and [Python Function Returning None](https://stackoverflow.com/q/21471876/4518341) – wjandrea Apr 06 '20 at 21:32

0 Answers0