This question already has answers here:

Why does my recursive python function return None? [duplicate]
(4个答案)
我真的不明白,为什么代码
def isIn(char, aStr):
    ms = len(aStr)/2
    if aStr[ms] == char:
        print 'i am here now'
        return True
    elif char>aStr[ms] and not ms == len(aStr)-1:
        aStr = aStr[ms+1:]
    elif char <aStr[ms] and not ms == 0:
        aStr = aStr[0:ms]
    else:
        return False
    isIn(char, aStr)

print isIn('a', 'ab')

不停地退货。它会打印“我现在在这里”,但不会像下一行所说的那样返回真值为什么?

最佳答案

最后一行可能需要一个return

return isIn(char, aStr)

如果没有它,函数在终止时只返回None,而看不到return

07-24 15:21