Functions section 6 Practice makes perfect


I'm close but I don't understand what I'm doing wrong. The error message states that by_three(3) returned true instead of 27. May someone please tell me where I'm going wrong?

def cube(n):
    return n**3
def by_three(n):
    if n % 3 == 0:
       return True   
        return False


from step 4 of the instructions:

if that number is divisible by 3, by_three should call cube(number) and return its result

you only return true, you should return the cube of the number (careful, in your case the number is in the variable n)


Why is it necessary to have " 3 == 0?" Specifically the ==0 piece.I am interpreting this as IF number divided by 3 equals 0 then return cube of that number....How should I be interpreting this line? I didn't have it in there before but after reading the questions above I am now curious. Thank you for any insights anyone can provide.


well, in the end it matters if the condition evaluates to true, if this happens via comparison or simply evaluate a number, doesn't matter.

but then its important to understand this:

if 1:
    print True
    print False
if 0:
    print True
    print False

so, 1 is true, and 0 is false (2 is also true, positive integers are evaluated as true)

so, you can do without == 0 but then for numbers not divisible by three if condition would be true, so you need to return False and else (number is divisible by 3) cube of number