Don't understand a few things about Functions 6: "Practice Makes Perfect"


#1



Hi everyone,

I'm stuck on Functions: Function Syntax: 6. Practice Makes Perfect.


I shouldn't say I'm stuck - rather, I found the right answer in an archived post in the Q&A forum, but I don't understand how the code that person supplies works. I tried my own solution first, and of course it didn't work.

First, if you're doing division, shouldn't the sign in the code below be / and not %? For that matter, what does % mean? I thought it was the symbol for a function, not the division symbol. Second, why is it "if number % 3 == 0:" and not "if number % 3 == True"? You are checking if it's divisible by three or not, so...how would that not be a boolean? There are obviously a few points I'm not getting here.

Thanks



#2

% is the modulo operator, it calculates the remainder. the modulo is explained really early in the course link

if a number is divisible by x (in this case 3) the remainder is zero, that is why we check if the remainder equal zero


#3

First as You posted the 'full' answer which is against guidelines of CC.
I'd suggest to edit your post so no one can just copy ,paste and use it.

Now back to your question...

First, if you're doing division, shouldn't the sign in the code below be / and not %?

When you divide a number.

say if X is divided by Y,

You get two things.

X / Y -- > Quotient say Q

and

X % Y --> Remainder say R

modulo is used to find this R,It means It gives you remainder not quotient.

Example..

13 / 5 -- > gives you Quotient 2.
but
13 % 5 --> gives you Remainder 3.

One thing will make things more clear..

What is division? continued subtraction ? right?

Lets say we are doing this..

13 / 5...
so we do this..

13 - 5 = 8
8 - 5 = 3 as now 3 is less than 5 so we can't subtract more .
so number of times we subtract is called doing /
and whatever remains is called finding %
here..
so your Q is 2 (as You can subtract two times)
and your R is 3(finally left value)
One question.
Does / makes sure that a number is divisible by other number?
Now lets see one more example..

15 / 3

15 - 3 = 12
12 - 3 = 9
9 - 3 = 6
6 - 3 = 3
3 - 3 = 0

Your Q is 5(as you're dividing 3 five times from 15)
Your R is 0(because there is nothing left)

You can see Q is not 0 but 5
As remainder is 0 when a number divides other.

so when Remainders gets 0,we definitely/surely can say that a number is totally divisible by other.

"if number % 3 == True"?

4 - 4 == 0 will return True or False? True

0 == 0 will return True of False? True

what this will return..

True == 4?
False.
(There are scenarios when different data types comparison leads to True ,in this example,its not)

In your example..

number == True always produceFalse.


#4

I've got to go to work now, but I'll respond more to this later this evening. I've removed the code in my original post.


#5

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