递归代码返回无 [英] Recursive code returns None

查看:22
本文介绍了递归代码返回无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白,为什么是代码

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.

这篇关于递归代码返回无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆