0

I'm trying to declare variables based off a number pulled from an input file. I tried using arrays and didn't get far.

amountOfQuads = header_line.split(',')[1]
print amountOfQuads
quad = []
print 'test'
for i in range(1,amountOfQuads):
    quad[i] = vtk.vtkQuad()

This gives a "range() integer end argument expected, got str" error.

For example, if there's 3, I could just do:

quad1 = vtk.vtkQuad()
quad2 = vtk.vtkQuad()
quad3 = vtk.vtkQuad()

This would work very well for just 3 squares, but not so much for larger amounts.

Is there a better way to do this?

davidism
  • 121,510
  • 29
  • 395
  • 339

2 Answers2

5

amountOfQuads is a string, but range needs an integer. Also, rather than setting indexes on the list, append to the list.

amountOfQuads = int(amountOfQuads)
quad = []
for i in range(amountOfQuads):
    quad.append(vtk.vtkQuad())

You can just replace this with a comprehension.

quad = [vtk.vtkQuad() for _ in range(int(header_line.split(',')[1]))]
davidism
  • 121,510
  • 29
  • 395
  • 339
2

You want to append them to the list

amountOfQuads = int(header_line.split(',')[1])
print amountOfQuads
quad = []
print 'test'
for i in range(amountOfQuads):
        quad.append(vtk.vtkQuad())

Or using list comprehension

quad = [vtk.vtkQuad() for _ in range(amountOfQuads)]
sedavidw
  • 11,116
  • 13
  • 61
  • 95
  • I assume the downvotes are because while you've identified the use of `.append` - you've left the error coming from `range` the same as the OP – Jon Clements Jun 24 '15 at 18:49
  • Which was not in the question....and not even mentioned in comments when I wrote this. Answer has been updated – sedavidw Jun 24 '15 at 18:50
  • The OP added a comment instead of an edit 13 mins ago saying: *Edit: Forgot, this bit of code also gives a "range() integer end argument expected, got str." error* – Jon Clements Jun 24 '15 at 18:52