Help with Practice Makes Perfect


#1

I am stuck on Practice makes perfect. Here's the link:
https://www.codecademy.com/courses/python-beginner-c7VZg/1/5?curriculum_id=4f89dab3d788890003000096#

This is my code:

def cube(number):
     return cube(6)
    
def by_three(number):
    if n % 3 == 0:
        print result
    else:
        print False

The error message comes up as:
Oops, try again. cube(1) resulted in an error: maximum recursion depth exceeded

First of all, what does the error message mean?

Second, What did I do wrong with my code?

Third, How do I fix it?

A big thank you to whoever can answer my questions!
`


#2

it means the function keeps calling/executing itself, a function calling itself is called recursion. (you can google this if you like), it is cause by this two lines:

def cube(number):
     return cube(6)

it is sort of a infinity loop, but slightly different.

the cube function should return number ^ 3, i suggest applying this to make your code overcome this error message (note: number ^ 3 is math syntax, not python syntax)


#3

Ok,thank you. Truly appreciate your help!!


#4

Did you resolve it? If so, that would be good

If not, see if you can solve it with this new insight i provided, if still stuck after trying this, post an updated version of your code and i will help you further


#5

No, I didn't.....once I tried to apply what you had said, I got stuck without ever reformatting my code. I couldn't understand how I would make the code return number^3.


#6

well, till the power is nothing of multiplying the number by itself a number of times, in this case 3 times:

number * number * number

or you can use ** which allows you to use exponent


#7

OK ......

def cube(number):
    return number ** 3

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

Error message: Oops, try again. by_three(3) returned 3 instead of 27


#8

step 4 of the instructions:

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

You just return number, not the cube of the number


#9

Alright, I got it..... thank you so much for your help!:slight_smile:


#10