It's good to make a guess at a solution to your problem, but take care in framing your proposed solution as your question. What you're trying to do is count down, then up, rather than stop recursion. Stopping recursion would require adding exit() as your recursive base case, and this is inappropriate; normal program execution lets the call stack resolve naturally (i.e. return to its caller). Modifying sys.setrecursionlimit(10) isn't recommended, either, because it artificially manipulates a global limit which is easily done with a proper base case conditional.
Here's one approach: print n on the way down the recursive call stack, then print n on the way back up once your base case has been reached. In other words, each function immediately prints its number, but doesn't print its number a second time until all recursive functions below it have printed their number(s) and resolved.
For example, the first function called, bounce(4) prints 4 immediately, then waits for bounce(3), bounce(2), bounce(1) and bounce(0) to do their work and return. That done, bounce(4) finally prints 4 once more before returning to the calling scope. All called functions behave the same (bounce(0) is a little different; we have to conditionally limit it to a single print to meet the requirement--if n: tests whether the number is non-zero).
def bounce(n):
if n >= 0:
print(n)
bounce(n - 1)
if n:
print(n)
if __name__ == "__main__":
bounce(4)
Output:
4
3
2
1
0
1
2
3
4
If you're having trouble understanding how the call stack works, try adding indentation to show how deep you are (time increases top to bottom, recursive depth increases left to right).
def bounce(n, depth=0):
if n >= 0:
print("{}{}".format(" " * depth, n))
bounce(n - 1, depth + 1)
if n:
print("{}{}".format(" " * depth, n))
if __name__ == "__main__":
bounce(4)
initial call, bounce(4)
|
v
4 base case, bounce(0)
3 |
2 v
1
0
1 <-- heading back up the call stack, bounce(1)
2
3
4