Quote:
Python, how an instruction can be done eventhough the recursion shouldn't allow it?
Your code flow is wrong.
Try :
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
<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)
[][][][][]