Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 递归函数不返回True或False,但正确流动并工作?_Python_String_Recursion_Boolean - Fatal编程技术网

Python 递归函数不返回True或False,但正确流动并工作?

Python 递归函数不返回True或False,但正确流动并工作?,python,string,recursion,boolean,Python,String,Recursion,Boolean,问题的措辞如下: 编写一个名为double letters的递归函数,该函数使用一个字符串参数astr。如果astr是包含“双字母”(同一字母连续出现两次)的字符串,则functin返回True;否则返回False。例如,双字母(“hello”)返回True,其中as双字母(“hi there”)返回False --没有要求任何人为我做这项工作,但这是我所拥有的。我知道它的流程是正确的,就像我将return True替换为Print('True'),反之亦然,它将打印这些内容。递归函数不能很好地

问题的措辞如下: 编写一个名为double letters的递归函数,该函数使用一个字符串参数astr。如果astr是包含“双字母”(同一字母连续出现两次)的字符串,则functin返回True;否则返回False。例如,双字母(“hello”)返回True,其中as双字母(“hi there”)返回False

--没有要求任何人为我做这项工作,但这是我所拥有的。我知道它的流程是正确的,就像我将return True替换为Print('True'),反之亦然,它将打印这些内容。递归函数不能很好地处理布尔值,还是我遗漏了一些显而易见的东西

def double_letters(astr):
    if len(astr) >= 2:
        if astr[0] == astr[1]:
            return True
        else:
            double_letters(astr[1:])
    else:
        return(False)
在一条线上,它本身并没有什么作用。您的意思是
返回双字母(astr[1:])


否则你会递归调用你的函数,但你会放弃它的返回值,而你的函数实际上会返回

,谢谢!我会投票给你们所有人,但我对这个网站还是新手,再次感谢!
double_letters(astr[1:])
        else:
            return double_letters(astr[1:])