Can someone explain step by step what is going on in <Practice makes Perfect (6/19)>?


#1

Can someone explain step by step what is going on in "Practice makes Perfect (6/19)"?

def cube(number):
    return number ** 3

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

#2

Hi @pearlexx ,

Here's the code that you posted, with a line added that calls the by_three function, with 6 as an argument ...

def cube(number):
    return number ** 3

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

print by_three(6)

Output ...

216

Explanation

This statement calls the by_three function ...

print by_three(6)

It assigns 6 to that function's parameter, number.

This line finds that the remainder is 0, so the condition is True ...

if number % 3 == 0:

Consequently, this statement executes, which calls the cube function, assigning 6 to that function's number parameter ...

return cube(number)

The cube function computes and returns the value 216, here ...

return number ** 3

... and when the by_three function receives back that result, it returns it, and the original statement that called the by_three function prints that result.


#3

The HUGE Thanks to you for explanation and especially for that added line.
That clears it!


#4

Is there another way to go about explaining this problem. I can't figure it out? Help


#5

To explain it in short,

your by_three function is checking to make sure that the number being passed through is divisible by 3,

def by_three(number): // Creating the function
if number % 3 == 0: // Remember % is checking remainder of the number input
return cube(number) // So this is doing many things at once, it is calling the other function to process which in turn returns the desired output
else:
return False // Obviously if the number doesn't return the remainder of 0 this is false

Now in the other function here is what is going on,

def cube(number): // It is getting the parameter (number) from by_three parameter of number to use
return number ** 3 // Remember ** is exponent, so what ever number is in psuedo is number ^ 3 and then returns that value back to by_three return statement and if you printed it, it would come out cubed

Hopefully that helped, let me know if you have anymore questions.