Weird bug involving -= acting on list elements

12 views
Skip to first unread message

Daniel Schroeder

unread,
Oct 21, 2025, 2:33:03 PMOct 21
to Glowscript Users
A student of mine has discovered a weird bug involving the -= operator acting on a list element. Here's an example:

x = [10]
y = 8
z = 3
x[0] -= (y+z)
print(x[0])

The printed value should be -1, but Web VPython prints 5, apparently because it subtracts z from y instead of adding, before subtracting the combination from x[0].  Changing y+z to y-z also produces an incorrect result (it then adds instead of subtracting), but I get a correct result when I change y+z to y*z.  Changing -+ to += followed by a unary - sign gives the correct result, as does writing x[0] = x[0] - (y+z).  I haven't been able to reproduce the error when -= acts on a variable that isn't a list element.


If this bug affects other types of expressions that I may be unaware of, I'd very much like to know about that.
Reply all
Reply to author
Forward
0 new messages