Need help with Exercise "Practice Makes Perfect"


#1

Hello world,

I'm stuck with this exercise, receiving the error notification as "cube(1) resulted in an error: maximum recursion depth exceeded". This is my code:

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

Can anyone please comment on my code?

Many thanks & Cheers,
Anh


#2

i don't understand your if statement:

if n == n ** 2:

could you explain the logic? Can you provide the exercise url as well? I need to have a look at the instructions/exercise.

uhm.. the error is caused by this line:

return cube(n)

inside your cube function, the function will endless keep calling itself. You should return n^3


#3

Hi Stetim94,

I created this statement because of the instruction No.2:

Make that function return the cube of that number (i.e. that number multiplied by itself and multiplied by itself once again).

Here is the link please:

https://www.codecademy.com/en/courses/python-beginner-c7VZg/1/5?curriculum_id=4f89dab3d788890003000096#

Can you please have a look?

Thank you,
Anh


#4

yea, so you should return n ^ 3 (n * n * n), so not sure why you need a if statement for it?


#5

So i mis intepreted it? let me try again


#7

I got it thru. Many thanks to you :slight_smile: