Oops, try again. by_three(2) resulted in an error: maximum recursion depth exceeded


#1

def cube(number):
return number*number*number
def by_three(number):
if cube(number)/3:
return by_three(number)
else:
return False

Returns the following:
Oops, try again. by_three(2) resulted in an error: maximum recursion depth exceeded


#2

Within the by_three function, you

return by_three(number)


You just want to check if the number passed into the function is divisible by three, and if it is you want to return it. You should just

return number

because by returning by_three(number), you are putting yourself into an infinite recursion call (a bit advanced, but basically your function will continue to call itself until it runs out of memory because there's no way out of it).


#3

When I change the by_three(number) to number in line 5, there is a error showing by_three(3) returned 3 instead of 27. Why this happens? How to figure it out?


#4

I looked again at the problem, looks like I missed something. You have to see if number is evenly divisible by three, then if it is, return the cubed value of the number. Try changing your by_three function to reflect this. Also, as a side note, you can change your return statement in your cube() function to use the power operator, (x**3 == x * x * x).


#5

Thank you. I figured it out.