6. Practice Makes perfect


#1

So my code was

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

I know the result is

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

my question is why do I need the == 0:?


#2

The remainder when divided by 3 is zero if the number is evenly divisable. Your first example would work if you switch the outcomes.

if number % 3:      # means if result is NON-ZERO
    return False
else:
    return cube(number)

#3

Oh that makes sense! Thank you


#4

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