Python Forum

Full Version: Unequal values when added with 0?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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]
      )
Operator precedence. % is done before +. Use parenthesis to control order of execution (6+0)%6