Maximum Recursion Depth Exceeded?


#1

This is my code:

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

print cube(11)
print cube(12)

I see nothing wrong with my code but it says maximum recursion depth exceeded.


#2

You are infinitely running a function inside of itself, but Python stops you so you don't crash.


#3

How would I fix this?


#4

Don't call the function inside of itself, but use an expression on the number to achieve desired output.


#5

I appreciate the help but I'm very new to coding so I'm not exactly sure what you mean, specifically by using an expression on the number.


#6

Examples:

#Multiply by 2
return number * 2
#Divide by number of elements
return number / len(elements)
#Square a number
return number ** 2
#Add 87
return number + 87
#Multiply by π
return number * 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367

#7

Then didn't I use an expression when I told it to return cube(number) ** 3


#8

You did, but you used the function inside of itself.


#9

Why'd it tell me to define cube(number) and make it return the cube? Would if work if I replaced cube(number) in the second line with number?


#10

Replacing cube(number) in the second line with just number worked. Thanks!


#11

'The cube' is a number to the power of three. You are writing a function to make it easier.

Read what I said, and if you still don't get it.

Click Here

Correct Code:
def cube(number):
return number ** 3