填充数组 - 在'for'语句中获取语法错误

时间:2012-07-24 19:41:53

标签: python for-loop syntax-error

我试图弄清楚为什么在尝试使用此程序时出现语法错误。当我使用填充列表时,此代码工作正常,但是,我决定使用数组,因为我可以根据自己的喜好操作数据。

以下是代码:

#Frame Creation

frames=[]

for n in range (0, (workingframes*archnodes*3)):
    frames.append( )


frames = array(frames)                                    #generates array
frames = reshape(frames, ((archnodes*3),workingframes)    #reshapes array




#Frame Population

for f in range (0, workingframes):

    if f<=(workingframes/2):

        for x in range (0, (archnodes)):
            frames[(archnodes*3)].insert((archnodes*3), (archstartred[x]))
            frames[(archnodes*3)+1].insert(((archnodes*3)+1),(archstartgrn[x]))
            frames[(archnodes*3)+2].insert(((archnodes*3)+2),(archstartblu[x]))

        for y in range (0, nodesperframe):
            archstartred.pop()
            archstartgrn.pop()
            archstartblu.pop()
            archstartred.insert(0, backred)
            archstartgrn.insert(0, backgrn)
            archstartblu.insert(0, backblu)

    else:
        for y in range (0, nodesperframe):
            archstartred.pop(0)
            archstartgrn.pop(0)
            archstartblu.pop(0)
            archstartred.append(backred)
            archstartgrn.append(backgrn)
            archstartblu.append(backblu)

        for x in range (0, (archnodes)):
            frames[(archnodes*3)].insert((archnodes*3), (archstartred[x]))
            frames[(archnodes*3)+1].insert(((archnodes*3)+1),(archstartgrn[x]))
            frames[(archnodes*3)+2].insert(((archnodes*3)+2),(archstartblu[x]))

我一直收到这个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "backandforth3.py", line 99
    for f in range (0, workingframes):
                                     ^
SyntaxError: invalid syntax

我尝试过为'f'使用不同的值。我尝试删除冒号(导致其他语法错误。)我缺少什么?

2 个答案:

答案 0 :(得分:4)

问题在于:

frames = reshape(frames, ((archnodes*3),workingframes)    #reshapes array

您有三个(和两个)。 Python认为reshape()调用在行尾没有结束,所以它继续向下移动直到它到达for:,这在函数内部没有意义调用

答案 1 :(得分:2)

缺少parens:

frames = reshape(frames, ((archnodes*3),workingframes)