Modulo operator giving strange answer with positive intgers

I can’t figure out why this is happening. Python 3 modulo operator is saying 6 % 5 = 4. Surely it should be 1? The integers are stored in variables, so I stepped through in debugging to make sure they really were 6 and 5 - yep. I even printed out the numbers and the modulo equation to make sure the variables weren’t somehow being changed before they were read, and it’s definitely 6 % 5 = 4:
print(var1, var2, var1%var2)output: 6 5 4

Do I seriously misunderstand how modulo works? I thought it was the remainder after division of unsigned integers. Can anyone shed light? If it helps, I am using the latest stable version of VS Code and Python 3.9.13.

If it helps, I closed VS Code, opened it again and this time ran it with var1 and var2 both equal to 5. Now the output is even odder:
print(var1, var2, var1%var2)output: 5 5 5

Is this something anyone has come across?

Solved it! So stupid… this is why you don’t code when tired. I had thought I had written %, but I had used the bitwise operator &. Sorry!

1 Like