1

I'm trying to create an array of spheres in VPython, each with a manually entered position. Something like:

ball[0] = sphere(pos=vector(-1,4,9))
ball[1] = sphere(pos=vector(-2,6,6))
ball[2] = sphere(pos=vector(0,6,1))

etc. The problem is that I keep getting an error reading "IndexError: list assignment index out of range". How can I resolve this issue?

John Doe
  • 159
  • 1
  • 1
  • 5

1 Answers1

0

see How to declare an array in Python?.

You also might want to consider using a compound

Modified code:

# https://stackoverflow.com/questions/56461496/how-to-create-an-array-of-shapes-in-vpython
from vpython import *
# https://stackoverflow.com/questions/1514553/how-to-declare-an-array-in-python
ball=[]
ball.append(sphere(pos=vector(-1,4,9)))
ball.append(sphere(pos=vector(-2,6,6)))
ball.append(sphere(pos=vector(0,6,1)))

Result:

enter image description here

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186