0

Why % always return 1

x=2 y=3 x,y=y,x print((x+6)%y) why the output is 1 last night I encounter 1%213 and 1%215 both calling out 1 not sure why?

19th Nov 2019, 10:43 AM
THEGreatGatsby
THEGreatGatsby - avatar
6 odpowiedzi
+ 4
It's basic math, if 0<a<b, then a%b = a So here you have a=1, and all the numbers you tested mod on are superior to a, so output will always be a (which is 1 in this case) It should be the opposite, for example: 9%2 = 1 (9 = 1 + 2×4) 11% 4 = 3 (11 = 3 + 2×4) And so on
19th Nov 2019, 10:45 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 8
No, because in this line: x, y = y, x You switched the values of x and y, so x now becomes 3, and y becomes 2, if we calculate it now: x + 6 is 9 9 % 2 is 1 So the answer is correct and as expected.
19th Nov 2019, 10:51 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
ok, but here we have x+6 so =8 and y is 3, so why is not 2 but 1 ?
19th Nov 2019, 10:49 AM
THEGreatGatsby
THEGreatGatsby - avatar
+ 1
thanks
19th Nov 2019, 10:53 AM
THEGreatGatsby
THEGreatGatsby - avatar
+ 1
No problem 👌
19th Nov 2019, 10:53 AM
Aymane Boukrouh
Aymane Boukrouh - avatar