简体   繁体   English

为什么这个递归 function 中的第二个条件出现两次

[英]Why does the 2nd condition in this recursive function occur twice

Trying to use recursion for a maze problem and made a conditional test case.尝试使用递归解决迷宫问题并制作条件测试用例。 Can someone explain to me why the 2nd condition adds 20 bs instead of 10?有人可以向我解释为什么第二个条件增加了 20 个 bs 而不是 10 个?

def recur_test(count, list1, status):
    if count == 10 and status == 1:
        return (count, list1)
    if count == 10 and status == 0:
        count = 0
        status = 1
        recur_test(count, list1, status)

    if count < 10 and status == 0:
        count += 1
        list1.append("a")
        print("A thread", count)
        recur_test(count, list1, status)

    if count < 10 and status == 1:
        count += 1
        list1.append('b')
        print("B Thread", count)
        recur_test(count, list1, status)

print(recur_test(0, [], 0))

Consider a simpler example:考虑一个更简单的例子:

def recur_test(value):
    if value:
        print("in first condition")
        recur_test(False)

    if not value:
        print("in second condition")

recur_test(True)

The way to understand this most easily is to substitute in another function that does the same thing, and call that instead of making the recursive call:最容易理解这一点的方法是替换另一个 function 做同样的事情,并调用它而不是进行递归调用:

def recur_test_2(value):
    if value:
        print("in first condition")
        value = False
        recur_test_2(value)

    if not value:
        print("in second condition")

def recur_test_1(value):
    if value:
        print("in first condition")
        value = False
        recur_test_2(value)

    if not value:
        print("in second condition")

recur_test_1(True)

Now there should be no confusion: when the call to recur_test_2 returns, recur_test_1 keeps going, and prints the in second condition message a second time - since value has been changed.现在应该没有混淆:当对recur_test_2的调用返回时, recur_test_1继续运行,并第二次打印in second condition消息 - 因为value已更改。

The same fundamental thing happens with recursion - each call to the function is "separate", it just happens that the function has the same name each time.递归也会发生同样的基本事情——对 function 的每次调用都是“单独的”,只是碰巧 function 每次都具有相同的名称

Your own code has the same issue - on the call where count == 10 and status == 0 , the one where you flip status to 1 , on the way "in" this switches from appending a s to appending b s, but on the way "out" those calls continue - and since count was reset to 0 in that call , it is now 0 again, and status is similarly 1 , so the b -appending branch is entered again.您自己的代码也有同样的问题 - 在count == 10 and status == 0的调用中,您将status翻转为1的调用,在“进入”的过程中,它从附加a s 切换到附加b s,但是在“退出”这些调用的方式继续 - 并且由于在该调用中count被重置为 0 ,它现在再次为0 ,并且status同样为1 ,因此再次进入b附加分支。

There is a further complication, though: in your code, the reassignments to count and status in further-on recursive calls don't matter to the current call, but the .append to list1 does - because it is the same list object , being modified in-place.但是,还有一个更复杂的问题:在您的代码中,在进一步递归调用中对countstatus的重新分配与当前调用无关,但是list1.append确实如此- 因为它是同一个列表 object ,即就地修改。

Your if statement unexpectedly falls through to each of the others even when it is true.即使它是真的,你的 if 语句也会意外地落入其他每个人身上。 When the second condition if count == 10 and status == 0: is true, you set count = 0 and status = 1, then make the recursive call.当第二个条件if count == 10 and status == 0:为真时,设置 count = 0 和 status = 1,然后进行递归调用。 The recursive call goes all the way through the "B Thread" and returns control to where you made the recursive call in that second if body.递归调用一直通过“B 线程”并将控制权返回到您在第二个 if 正文中进行递归调用的位置。 So now count and status are 0 and 1 respectively.所以现在 count 和 status 分别是 0 和 1。 This makes the last condition if count < 10 and status == 1: true, so it continues to go all the way through the "B Thread" again. if count < 10 and status == 1: true,则这使得最后一个条件为真,因此它再次通过“B 线程”一直继续到 go。

I would suggest changing the if statements to if else.我建议将 if 语句更改为 if else。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM