开发者

Python 3 with the range function

开发者 https://www.devze.com 2022-12-25 04:07 出处:网络
I can type in the following code in the terminal, and it works: for i in range(5): print(i) And it will print:

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/impor开发者_Go百科t_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')


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号