6.Practice Makes Perfect assistance please

This is the code I successfully ended the lesson with:

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

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

I understand the end product of the code to get passed this lesson. I just don’t really follow why I need to have 3==0:
Example:
This is incorrect if(number)%3:
This is correct if(number)%3==0:

Please explain to me why I need the ==0: in there?

Hope I kept this organized neatly for you guys, thanks in advance :grin:

2 Likes

@pvmsupra,

The so-called modulo / remainder-operator if used it will return the rest-value.

9%3 You read it as nine modulo three is zero, as 9 divided by 3 has NO rest-value.
22%6 You read 22 modulo six is 4, as 22 divided by 6 will leave you with rest-value 4
16%8 You read it as sixteen modulo eight is zero, as sixteen divided by 8 has NO rest-value

Thus if you use the modulo operator in a condition,
where the result should give a Boolean Value of true or false
you will use a comparison
like

number%2 == 0

if true, the number would be an =even= number.

Reference:

google search
== the Book ==
modulo operator site:python.org

== discussions / opinions ==
python modulo operator explained site:stackoverflow.com

3 Likes

This code didn’t work for me.

What is Fals? Check spelling carefully. :wink:

Can you tell me if this is an accurate way of rephrasing part of your post?

The ==0: is required to ensure that a whole number is the result of the algorithm.
With - if (number) % 3==0: - you filter out any division that would result in there being a remainder.
Example: 5/2 (1 remains because 2 goes in twice - leaving 1.)

If there is any rest-value(going back to the modulo) then it is not a number that the code will process because it will be false.

I was having some trouble with this… I am so grateful for these forums. I am completely novice with coding so it helps a bunch.

2 Likes

def cube(number):
return (number)**3
def by_three(number):
if (number) % 3==0:
return cube(number)
else:
return False
This didn’t work for me

@trlee20,
As soon as you reach a return statement
you will EXIT the function…

In this case the effective FUNCTION would only consist of

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

as the rest would NEVER be reached/executed…!!!

Thank you:heart_eyes:

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

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