python,即使递归不应该允许递归允许递归,该指令如何完成?
在此功能中:
def print_triangle (sideLength):
if sideLength < 1 :
return
print_triangle (sideLength-1)
print ( "[]"* sideLength)
指令:
print ( "[]"* sideLength)
永远不应达到指令,因为指令:
print_triangle (sideLength-1)
将阻止它在不断调用函数的情况下达到该功能,这将使程序的流程再次发送到函数的开头,也就是说顶部。 因此,它将继续执行此操作,
print ( "[]"* sideLength)
直到变量sidelength
变为零,它将继续执行此操作。
然而,
print ( "[]"* sideLength)
当您将其称为4作为参数时,可以到达并绘制一个三角形...但是如何呢?
In this function:
def print_triangle (sideLength):
if sideLength < 1 :
return
print_triangle (sideLength-1)
print ( "[]"* sideLength)
The instruction:
print ( "[]"* sideLength)
Should never be reached, because the instruction:
print_triangle (sideLength-1)
Would be preventing it to be reached as it keeps calling the function, which would send the flow of the program again to the beginning of the function, that is to the top. Instead of allowing it to keep going below to the:
print ( "[]"* sideLength)
Hence, it will continue doing that until the variable sideLength
becomes zero.
And yet the line:
print ( "[]"* sideLength)
Is reached and draws a triangle when you call it with say a 4 as a parameter... but how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以想象下面的递归呼叫“树”。每个框代表函数的执行上下文,因此每次进行新调用时,都会描绘一个新框。内部最大的框表示参数为0时的情况,在这种情况下,函数将返回,关闭内部框。但是,该最终框的呼叫者将能够继续,并继续使用
print
指令。因此,这是最初的电话:
You can imagine the recursive call "tree" as below. Each box represents an execution context of the function, so each time a new call is made a new box is depicted. The inner most box represents the case when the argument is 0, in that case the function will return, closing the inner box. But then the caller of that innermost box will be able to continue, and it will continue with the
print
instruction.So here is the initial call:
好吧,想一想。
首先,它以4个开始
,在打印
[]*4
之前,然后再次调用函数,这次是3:现在,在打印
[]*3 <之前,它是 3:现在。 /code>,然后再次调用函数,这次是2:
现在,在它打印
[]*2
之前,然后再次调用函数打印
[]*0
,然后再次调用函数,这次以0为0。现在,我们知道零小于1,它返回到最后一个功能,或1。
现在我们去向后回到最后一个功能调用。
然后是在此之前的一个:
然后是一个之前:
最后您第一次呼叫该功能:
Well, think it through.
First, it starts with 4
Now, before it prints off
[]*4
, it then calls the function again, this time for 3:Now, before it prints off
[]*3
, it then calls the function again, this time for 2:Now, before it prints off
[]*2
, it then calls the function again, this time for 1:Now, before it prints off
[]*0
, it then calls the function again, this time for 0.Now we know that since zero is less than 1, it returns to the last function, or 1.
Now we go backwards to the last function call.
Then to the one before that:
And then the one before that:
And finally your first call of the function:
它执行 返回基本案例,并且功能调用的堆栈开始弹出。
如果按照以下方式写入它,则交换最后两行:
区别将是打印的三角形将颠倒。
It executes after the base case is returned and the stack of function calls start to pop.
If you write it as follows, swapping the last two lines:
The difference will be that the printed triangle will be upside down.
是不正确的。首先,“它不断调用功能”是错误的。这仅调用一次函数。当功能返回时,它将继续到下一行。
特别是,
只要
sidelength
到达0
函数调用将返回。作为一个更具体的示例,让我们看看当您调用
print_triangle(3)
时会发生什么。首先,我们检查如果sidelength&lt; 1
是错误的,因此我们转到print_triangle(sidelength-1)
。这是递归调用
print_triangle(2)
。同样,我们检查如果Sidelength&lt; 1
。仍然跌倒,因此我们再次使用print_triangle(1)
再次浏览。如果Sidelength&lt; 1
仍然是错误的,因此我们再用print_triangle(0)
再一次重复一次。但是现在
如果sidelength&lt; 1
是真的,所以我们返回。这使我们回到了sidelength
的呼叫中,但在递归呼叫之后。要执行的下一行代码是print(“ []* sidelength)
,这是执行的方式。打印后,我们再次从函数返回到
sidelength
为2。到3。然后再打印,然后再从顶级呼叫返回。This is incorrect. First, "it keeps calling the function" is wrong. This only calls the function once. When the function returns, it will continue to the next line.
In particular, you have
So as soon as
sideLength
reaches0
the function call will return.As a more concrete example, let's look what happens when you call
print_triangle(3)
. First, we checkif sideLength < 1
, which is false, so then we go toprint_triangle (sideLength-1)
.This is the recursive call
print_triangle(2)
. Again, we checkif sideLength < 1
. Still falls, so we recurse again withprint_triangle(1)
.if sideLength < 1
is still false, so we recurse one more time withprint_triangle(0)
.But now
if sideLength < 1
is true, so we return. This puts us in back to the call wheresideLength
is 1, but after the recursive call. The next line of code to execute isprint ( "[]"* sideLength)
, which is how it gets executed.After the print, we return from the function again back to when
sideLength
is 2. So weprint()
again and return back tosideLength
set to 3. And one more print before returning from the top-level call.如果您不返回值,则该函数将继续运行,然后重复。所以移动:
返回print_triangle(sidelength-1)
print> print(“ []”* sidelength)
。将使print()仅运行一次If you don't return the value then the function will continue running it will then repeat. So moving:
return print_triangle (sideLength-1)
Before
print( "[]"* sideLength)
. Will make print() only run once