Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
This function never stops, and Python ends it with a RecursionError. Why? Fix it.
def countdown(n):
print(n)
countdown(n - 1)
countdown(3)Every recursive function needs a base case.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
Why. There is no base case. The calls go and never stop. After about 1000 calls, Python stops it with a RecursionError.
Fix. Add a base case that stops the calls, and return:
Log in to join the discussion.
Loading…
With Standard, you also open 10 more problems, 2 Python labs and 1 extra course. Everything you have used here stays free, always.