Python Forum
Unequal values when added with 0? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Unequal values when added with 0? (/thread-44494.html)



Unequal values when added with 0? - Azdaghost - May-23-2025

Hello! I'm trying to make a rainbow cube and realized this
why does this output 0...
print(6%6)
but this outputs 6?
print(6+0%6)
I'm also using trinket.io/glowscript

full code:
import vpython as vp
from random import choice
scene = vp.canvas()

colors = [
  vp.color.red,
  vp.color.orange,
  vp.color.yellow,
  vp.color.green,
  vp.color.blue,
  vp.color.purple,
]

for x in range(6):
  for y in range(6):
    for z in range(6):
      print(6==6+0, 6%6==6+0%6)
      vp.box(pos=vp.vector(x-10, y-10, z-10),
        color=colors[x+y+z%6 if x+y+z%6<=5 else 5]
      )



RE: Unequal values when added with 0? - deanhystad - May-24-2025

Operator precedence. % is done before +. Use parenthesis to control order of execution (6+0)%6