Practice Makes Perfect

#1

def cube(number):

``return cube(number) ** 3``

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

can any one help me o solve this python code
when i hit save &submit it shows me like this

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

#2

The above is known as recursion which is the process of a function calling itself from within the same function. There is a special requirement in recursion called a base case which we won't go into right now. It's more important that you see it cannot be applied to this problem.

Recursion will come up in advanced topics, but not until then so set this concept aside and focus on the problem at hand.

Our function is to return the cubed value of the number given in the argument. Your code is nearly correct, in that you have `** 3` which is the right exponentiation. However, we wish to cube the number, and return it, not pass it back to the function.

``return number ** 3``

In the above. `number` does not need to be parenthecized.

``if number % 3 == 0``

#3

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

i got this statement when i apply. Is there any wrong in my program.

#4

#5

def cube(number):

``return number ** 3``

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

This is my revised code

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.