LearnFrenzy
Sign in Take a free test

Recursion · Find the Error · Question 7

Error: correct recursion that is too deep Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

The program below stops with an error. Which statement correctly describes the problem?

main.py
def total(n):
    return 0 if n == 0 else n + total(n - 1)

print(total(10_000))
  • A RecursionError: the logic is right, but 10,000 nested calls exceed Python's default limit of about 1000; use a loop or sum(range(n + 1)) Correct
  • B It prints 50005000
  • C MemoryError
  • D OverflowError
Correct Answer: Option A

Python keeps every frame until the base case is reached, and it has no tail-call optimisation.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links