views:

90

answers:

2

I can type in the following code in the terminal, and it works:

for i in range(5):
    print(i)

And it will print:

0
1
2
3
4

as expected. However, I tried to write a script that does a similar thing:

print(current_chunk.data)
read_chunk(file, current_chunk)
numVerts, numFaces, numEdges = current_chunk.data
print(current_chunk.data)
print(numVerts)

for vertex in range(numVerts):
    print("Hello World")

current_chunk.data is gained from the following method:

def read_chunk(file, chunk):
    line = file.readline()
    while line.startswith('#'):
        line = file.readline()
    chunk.data = line.split()

The output for this is:

['OFF']
['490', '518', '0']
490
Traceback (most recent call last):
  File "/home/leif/src/install/linux2/.blender/scripts/io/import_scene_off.py", line 88, in execute
    load_off(self.properties.path, context)
  File "/home/leif/src/install/linux2/.blender/scripts/io/import_scene_off.py", line 68, in load_off
    for vertex in range(numVerts):
TypeError: 'str' object cannot be interpreted as an integer

So, why isn't it spitting out Hello World 490 times? Or is the 490 being thought of as a string?

I opened the file like this:

def load_off(filename, context):
    file = open(filename, 'r')
+1  A: 

'490' is a string. Try int('490').

Ignacio Vazquez-Abrams
A: 

Sigh, never mind, it did turn out to by evaluated as a string. Changing the for loop to

for vertex in range(int(numVerts)):
    print("Hello World")

fixed the problem.

Leif Andersen