Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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-很难理解这个递归代码示例_Python_Python 3.x - Fatal编程技术网

Python-很难理解这个递归代码示例

Python-很难理解这个递归代码示例,python,python-3.x,Python,Python 3.x,我试图理解这段代码是如何工作的。这是基本的递归,但我很难理解这个概念 代码来自sololearn应用程序,我已经阅读了他们关于这个主题的教程,但没有理解这个概念。每次打印出函数的返回值时,都会得到一个布尔值,但我不明白函数是如何得到这个结果的。我可以运行代码并查看输出,但我需要知道它是如何工作的。爱你 def is_even(x): if x == 0: return True else: return is_odd

我试图理解这段代码是如何工作的。这是基本的递归,但我很难理解这个概念

代码来自sololearn应用程序,我已经阅读了他们关于这个主题的教程,但没有理解这个概念。每次打印出函数的返回值时,都会得到一个布尔值,但我不明白函数是如何得到这个结果的。我可以运行代码并查看输出,但我需要知道它是如何工作的。爱你

def is_even(x):
        if x == 0:
            return True
        else:
            return is_odd(x-1)

def is_odd(x):
        return not is_even(x)

print(is_odd(1))

我添加了一些有用的print语句,希望这有助于理解调用
时实际执行的是什么:

def is_even(x):
    if x == 0:
        print("return True")
        return True
    else:
        print("return is_odd({0}-1)".format(x))
        return is_odd(x-1)

def is_odd(x):
    print("return not is_even({0})".format(x))
    return not is_even(x)
print(单数)(1))

print(单数)(2))

也可以考虑在运行时交互式检查代码。

工具书类
Python调试器:

尝试跟踪执行,a(=>)的左侧指示对其中一个函数的新调用,右侧指示确定最终结果所采取的步骤

is_odd(1) => not is_even(1)
    is_even(1) => is_odd(0)
        is_odd(0) => not (is_even(0))
            is_even(0) => True
                  => not (True)
                  => False
               => False
          => not (False)
          => True
或者这可能更有帮助

is_odd(1)
=> not is_even(1)
=> not (is_odd(0)
=> not (not (is_even(0))
=> not (not (True))
=> not (False)
=> True
FWIW,这些函数被称为“相互递归”(即,多个函数相互调用)。如果您不习惯递归,您可能应该从简单的单一递归开始,常见的例子是fibonnaci数或阶乘函数

def fact(n):
    if n == 0: 
        return 1
    else: 
        return n * fact (n-1)

def fib(n): 
    if n == 0: 
        return 1
    elif n == 1: 
        return 1
    else:
        return fib (n-1) + fib (n-2)

所以,让我们把它分解成几个步骤

def is_even(x):
        if x == 0:
            return True
        else:
            return is_odd(x-1)

def is_odd(x):
        return not is_even(x)

print(is_odd(1))

1) we say print -> is_odd(1).  So we're sending a 1 to is_odd() function
2) is_odd() recieves the 1 and before it can return a value, it must call is_even() and send that same 1.
3) is_even() now recieves that 1 and checks if its equal to zero, but it fails, so it then has to call is_odd() and send it 1 - 1.  
4) is_odd checks the new value (zero in this case) and calls again is_even() sending that zero
5) is_even() now resovles at the if x == 0: line and returns True.  
6) Now we've reached the end of the recursive loop and all the functions will resolve with their return statments.  Starting with the is_even() being True.  
这是一个细分->

print(is_odd(1)) -> 
    print(NOT is_even(1))->
       print(NOT is_odd(1-1)) ->
         print(NOT NOT is_even(0)) -> 
           print(is_even(0)) -> 
               print(True) ->
                 True

我会在代码中加入显示所有阶段变量的
print()
语句。这将使您更好地了解各个部分是如何相互作用的。
def is_even(x):
        if x == 0:
            return True
        else:
            return is_odd(x-1)

def is_odd(x):
        return not is_even(x)

print(is_odd(1))

1) we say print -> is_odd(1).  So we're sending a 1 to is_odd() function
2) is_odd() recieves the 1 and before it can return a value, it must call is_even() and send that same 1.
3) is_even() now recieves that 1 and checks if its equal to zero, but it fails, so it then has to call is_odd() and send it 1 - 1.  
4) is_odd checks the new value (zero in this case) and calls again is_even() sending that zero
5) is_even() now resovles at the if x == 0: line and returns True.  
6) Now we've reached the end of the recursive loop and all the functions will resolve with their return statments.  Starting with the is_even() being True.  
print(is_odd(1)) -> 
    print(NOT is_even(1))->
       print(NOT is_odd(1-1)) ->
         print(NOT NOT is_even(0)) -> 
           print(is_even(0)) -> 
               print(True) ->
                 True