0
import sys

sys.setrecursionlimit(10)
def bounce(n):
   if n <= 0:
       print(n,)
       bounce(n+1)


   else:
       print(n)
       bounce(n - 1)
       if (n==0 ):
           n==False

I'm trying to create a countdown and countup program. I only have the option to use one function and I have to use a recursive function. But every time I run the program it wont do anything the this:

4
3
2
1
0
1
0
1
0
1
0
1
0
1
0
1 

What can I do to make the countup work like it is supposed to? It's supposed to look like this:

>>> bounce(4)
4
3
2
1
0
1
2
3
4
>>> bounce(0)

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! But I don't get it how "if n: print n" ,prints out 1,2,3,4?
Yeah! Thank you . Great explanation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.