2010-03-26 74 views
0

我可以在终端下面的代码键入,和它的工作原理:Python 3中与范围功能

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

它将打印:

0 
1 
2 
3 
4 

如预期。不过,我试着写一个脚本,做了类似的事情:

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从以下方法获得:

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

这个输出是:

['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 

那么,为什么它不是吐出Hello World 490次?或者490被认为是一个字符串?

我开这样的文件:

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

回答

2

'490'是一个字符串。尝试int('490')

0

感叹,没关系,它确实通过评估为一个字符串。将for循环更改为

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

修复了这个问题。