LearnFrenzy
Sign in Take a free test

Recursion · Find the Error · Question 1

Error: no base case Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

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

main.py
def factorial(n):
    return n * factorial(n - 1)

print(factorial(5))
  • A It prints 120
  • B RecursionError: there is no base case, so the calls continue past 0 into negative numbers until the depth limit; add if n <= 1: return 1 Correct
  • C It prints 0
  • D TypeError
Correct Answer: Option B

Every recursive function needs a case that returns without recursing.

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