Practice makes perfect


#1

Continuing the discussion from The Big If:

why doesn't this work. the error says cube1 and in the console it says None. have a look see what you all think.

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


#2

cube isn't defined.

cube isn't defined.

What are you trying to achieve with the cube() function?


#3

what do you mean ? CUBE isn't def


#4

cube is defined as cube

That's an infinite recursive loop though, it'll never terminate with a result.

@mickyblue74 make sure that your code can be copied to the same effect as you describe and post errors in their entirety instead of paraphrasing them! Make it abundantly clear what your situation and make information easily available and accurate - you get quicker and more accurate replies that way as more people will be willing to help and they'll have a better understanding of what is happening. I read this post and immediately react with: "No idea, moving on to a question with enough information to actually be answerable!"


#5

Remember how to write an exponent?

base ** exponent

so,

return number ** 3

#6

Hi

I have tried that but just cant get it to work any other ideas ? I'll try again if it works i'll get the beers in


#7

still no joy I've indented if and else twice and return four times. this is what I get: cube(1) resulted in an error: maximum recursion depth exceeded


#8

Be sure you are not calling the function from within itself. As @ionatan has said, it sets up an infinite loop. Study this example:

def cube(number):
    return number ** 3

#9

Thanks for getting back to me on this one. But I don't know what you mean when you say infinate loop. Could you give me an example. Thanks in advance


#10

def cube(n):
    cube(n)

See how inside the function it is calling the same function? That is known as recursion. When there is no exit condition or base case, the loop goes forever.

Internal error: RangeError: Maximum call stack size exceeded