Python递归函数返回Nonetype

Jer*_*her 0 python recursion

我有一个函数,递归搜索2d矩阵,找到值0并返回其位置.这是代码:

    def findNextZero(x, y, board):
       if board[x][y] == 0:
          return (x, y)
       else:
           if y == (SIZE-1):
              # if its at the edge of the "board", the 2d matrix
              findNextZero(x+1, 0, board)
           else:
              findNextZero(x, y+1, board)
Run Code Online (Sandbox Code Playgroud)

当我打印(x,y)时,该函数将打印正确的元组.但是,如果我尝试返回它,则表示返回值为None.为什么会这样?

Mar*_*ers 5

忽略了递归调用的返回值.return为这些添加语句:

def findNextZero(x, y, board):
    if board[x][y] == 0:
        return (x, y)
    else:
        if y == (SIZE-1):
            # if its at the edge of the "board", the 2d matrix
            return findNextZero(x+1, 0, board)
        else:
            return findNextZero(x, y+1, board)
Run Code Online (Sandbox Code Playgroud)

如果没有这些return,findNextZero()函数只会在没有显式返回任何内容的情况下结束,从而导致返回默认返回值.