Python Forum
Unequal values when added with 0?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unequal values when added with 0?
#1
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]
      )
Reply
#2
Operator precedence. % is done before +. Use parenthesis to control order of execution (6+0)%6
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python beginner that needs an expression added to existing script markham 1 1,486 Sep-04-2023, 05:24 AM
Last Post: Pedroski55
  [SOLVED] [BS] Why new tag only added at the end when defined outside the loop? Winfried 1 1,611 Sep-05-2022, 09:36 AM
Last Post: snippsat
  Why are two variables unequal as they point to the same value? fc5igm 4 3,809 Jun-08-2021, 02:41 PM
Last Post: fc5igm
  httplib2 - how to see credentials added by add_credentials? MSV 2 3,014 Aug-05-2020, 12:24 PM
Last Post: MSV
  How to show newly added column to csv johnson54937 3 3,139 Jan-07-2020, 04:01 AM
Last Post: Larz60+
  itertools.zip_shortest() fo unequal iterators Skaperen 10 9,833 Dec-27-2019, 12:17 AM
Last Post: Skaperen
  Additional slashes being added to string burvil 8 20,317 Aug-04-2017, 04:15 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020