Recursive code returns None(递归代码返回无)
本文介绍了递归代码返回无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我真的不明白,为什么是代码
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
.
这篇关于递归代码返回无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:递归代码返回无


基础教程推荐
猜你喜欢
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01