Predict Recursive Output
Beginner · Predict the output · from Python Recursion: How It Works, When to Use It, and Examples
From the lesson: Python Recursion: How It Works, When to Use It, and Examples
Imagine a set of Russian nesting dolls. You open the biggest one and find a smaller one inside. Open that, and there's an even smaller one. You keep opening until you reach the tiniest doll that doesn't open - that's the end.
Read the lessonRead this code and predict the output. Then write print() statements that produce the exact same output.
def mystery(n):
if n <= 0:
return
mystery(n - 1)
print(n)
mystery(3)Notice that the print(n) is after the recursive call. Think carefully about the order.
# Print the exact output of the code above
More Recursion exercises
- Recursive Sum 1 to NBeginner
- Fix the Broken Power FunctionIntermediate
- Recursive FibonacciIntermediate
- Reverse a String RecursivelyIntermediate
- Count Digits RecursivelyAdvanced
All Python exercises · 50 practice problems · Functions quiz