递归代码返回无
问题描述
我真的不明白,为什么是代码
I really do not understand, why the code
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')
确实继续返回 None.它打印我现在在这里",但它不返回 True,就像下一行所说的那样.为什么?
does keep on returning None. it prints 'i am here now', but it does not return True, just as the next line says. Why?
解决方案
你可能希望在最后一行有一个 return
:
You probably want a return
on the last line:
return isIn(char, aStr)
没有它,函数会在没有看到 return
的情况下终止时简单地返回 None
.
Without it, the function simply returns None
when it terminates without seeing a return
.
相关文章