Practice makes perfect


def cube(number):
return number**3

def by_three(number):
if number % 3 == 0:
return by_three(number)
return False

It shows: by_three(3) resulted in an error: maximum recursion depth exceeded. What does it mean? Anything wrong with my code?
P.S. I indent but it didn't show here.


@chipcoder10905 ,

To make indentation and other details visible, you should format code when you post it. See How do I format code in my posts?.

Instruction 4 states ...

if that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise, by_three should return False.

Instead of calling cube(number), your function calls by_three(number). A function's calling itself is known as recursion. In this particular case, the function calls itself repeatedly, until the maximum depth of recursion is reached. Recursion is useful in some situations, but when it is used, a base case needs to be defined that limits its depth.

Recursion should not be used for this exercise.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.