15,918,109 members
See more:
In this function:

Python
```def print_triangle (sideLength):
if sideLength < 1 :
return
print_triangle (sideLength-1)
print ( "[]"* sideLength)```

The instruction:

Python
`print ( "[]"* sideLength)`

Should never be reached, because the instruction:

Python
`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:

Python
`print ( "[]"* sideLength)`

Hence, it would continue doing that until the variable
Python
`sideLength `
becomes zero.

And yet the line:

Python
`print ( "[]"* sideLength)`

Is reached and draws a triangle when you call it with say a 4 as a parameter... but how?

Python
`print_triangle (4)`

Python
```[]
[][]
[][][]
[][][][]```

What I have tried:

I have seen numerous video-tutorials and read various website tutorials, but they just skip this particular kind of example, they do have similar examples but they just forget to explain this detail
Posted
Updated 13-Apr-22 20:53pm
v2

Solution 2

consider the simple case of the `print_triangle(1)` call
pseudocode
```print_triangle(1):
if 1 < 1 :
return //since sideLength=1, then  sideLength < 1 is false, hence the execution goes on
print_triangle(0) // inside print_triangle(0),  sideLength=0, then sideLength < 1 is true, hence it returns immediately
print ( "[]"* 1)//  the statement print ( "[]"* 1) is executed, producing a [] on the screen.```

Apply a similar line of reasoning to `print_triangle(2)` and so on, in order to discover the magic of recursion.

Patrice T 15-Apr-22 14:24pm
+5

Solution 1

Quote:
Python, how an instruction can be done eventhough the recursion shouldn't allow it?

Try :
Python
```def print_triangle (sideLength):
if sideLength > 1 :
print_triangle (sideLength-1)
print ( "[]"* sideLength)```

The principle :
As long we are not on top of triangle, call the function for a triangle minus 1 row.
Then print last row of current triangle.

Try this code to see the execution of your code
Python
```<pre>
def print_triangle (sideLength):
print ( sideLength, " def print_triangle (sideLength)")
print ( sideLength, " if sideLength < 1")
if sideLength < 1 :
print ( sideLength, " return")
return
print ( sideLength, " print_triangle (sideLength-1)")
print_triangle (sideLength-1)
print ( sideLength, " print ( ""[]"" * sideLength)")
print ( "[]"* sideLength)

print_triangle (5)```

Result is
```5  def print_triangle (sideLength)
5  if sideLength < 1
5  print_triangle (sideLength-1)
4  def print_triangle (sideLength)
4  if sideLength < 1
4  print_triangle (sideLength-1)
3  def print_triangle (sideLength)
3  if sideLength < 1
3  print_triangle (sideLength-1)
2  def print_triangle (sideLength)
2  if sideLength < 1
2  print_triangle (sideLength-1)
1  def print_triangle (sideLength)
1  if sideLength < 1
1  print_triangle (sideLength-1)
0  def print_triangle (sideLength)
0  if sideLength < 1
0  return
1  print ( [] * sideLength)
[]
2  print ( [] * sideLength)
[][]
3  print ( [] * sideLength)
[][][]
4  print ( [] * sideLength)
[][][][]
5  print ( [] * sideLength)
[][][][][]```

v4