0
If 2 goes into 5 two times, then 7%(5//2) = 7%2 which would be 3.5, but 3.5 isn't an answer.
I need an explanation...
4 odpowiedzi
+ 1
X%Y = X - (X//Y)*Y
7%2 = 7 - (7//2)*2 = 7 - 3*2 = 1
0
0
% isn't the division operator, it gets the *remainder* of a division
2 goes into 7 three times, and then gets the remainder: which is 1
Thus, 7 % 2 returns a value of 1
0
The % is modulo; it deals mainly with dividing whole numbers, and gives you the remainder when it reaches it's limit.
EG: 7%2
So first we see how many times 2 will fit in 7 as a whole number:
2 x 1 = 2 - still lower than 7
2 x 2 = 4 - still lover than 7
2 x 3 = 6 - still lower than 7
2 x 4 = 8 - higher than 7
Now to complete the modulo, we do 7 - 6 which equals 1, your result! Your program does these calculations for you, all you need to understand is you are seeing what's left over, not the result of a division.