简体   繁体   中英

Why does this function print the result, but return none?

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

This function prints the solved Sudoku, however print(solve(sudoku)) returns none. Why does the function return none if it can print(sudoku)?



def solve(sudoku):
    for y in range(9):
        for x in range(9):
            if sudoku[y][x] == 0:
                for n in range(1, 10): 
                    if possible(x, y, n):
                        sudoku[y][x] = n
                        solve(sudoku)
                        sudoku[y][x] = 0
                        
                return # this might be a problem
    print(np.matrix(sudoku))
    return sudoku

Your assumptions must be wrong.

If you are saying that print(solve(sudoku)) print None , then this must be what is happening.

The only way for that to happen is if the last return is from the third line from the bottom of your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM