"practice makes perfect" problem


def cube(number):
    def by_three(number):
        return number ** 3
    if number % 3 == True:
        return by_three
        print "n is divisible by 3"
        return False
        print "n isnt divisible by 3"

it keeps saying cube(1) resulted in an error: maximum recursion depth exceeded


so I changed it a bit but now it replies "cube(1) returned False instead of 1


There are two functions involved in this program. cube() and by_three().

def cube():

def by_three():

The cube function is straight forward enough, given a number argument.

return number ** 3

by_three requires some decision making, and an if..else scenario.

if number % 3: return False
return cube(number)

But wait, there is no else! Because we don't need one when the if case has a return statement. Flow essentially falls through the if and on to the final return statement.

Were there not a return statement in the if block, then else would be an absolute requirement. This is why return is such a great tool.




this helped a lot thank you for walking through it


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