Help! 6. practice makes perfect


#1



def cube(number):
return number * number *number

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


please anyone can tell me what's wrong with my code? It says "The program took too long to finish. Check your code for infinite loops and try again." Many thanks!!


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


#2

here:

return by_three(cube(number))

why call the by_three function inside the by_three function? This causes recursion, and given the recursion doesn't end, its a similair to a infinity loop


#3

Ah thank you! I got it right this time.

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

My misunderstanding is on the instructions "by_three should call cube(number) and return its result". So can I say "A should call B", means A should be replaced by B? I previous thought it means A(B).


#4

no, this simply means that inside the by_three function you have to call cube

this simply means that as function content (anything which is nested in the function) should call cube at some point.


#6

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