python 3.2语法错误:语法无效

时间:2012-05-29 19:42:44

标签: python syntax

任何人都可以在这个python 3.2代码中找到语法错​​误吗?

import math
import numpy

vinf = 10
vfr = 4
x = numpy.linspace(-10,10,50)
y = numpy.linspace(-10,10,50)
a = numpy.zeros(shape=(len(x),len(y)))
b = numpy.zeros(shape=(len(x),len(y)))
u = numpy.zeros(shape=(len(x),len(y)))
v = numpy.zeros(shape=(len(x),len(y)))
p = numpy.zeros(shape=(len(x),len(y)))
s = numpy.zeros(shape=(len(x),len(y)))


for i in range (0,len(x)):
    for j in range (0,len(y)):
        a[i][j]=x[i]
        b[i][j]=y[j]


for i in range (0,len(x)):
    for j in range (0,len(y)):

        v[i][j] = (vfr/(2*math.pi))*math.pow((b[i][j]+.0047)/(a[i][j]+.05)+(b[i][j]+.0517)/(a[i][j]+.2084)+(b[i][j]-.0271)/(a[i][j]+.2084)+(b[i][j]+.0583)/(a[i][j]+.525)+(b[i][j]-.0228)/(a[i][j]+.525)+(b[i][j]+.0242)/(a[i][j]+.8417)+(b[i][j]-.0087)/(a[i][j]+.8417),-2)*((b[i][j]+.0047)/(math.pow(a[i][j]+.05,2)*(1+math.pow((b[i][j]+.0047)/(a[i][j]+.05),2)))+(b[i][j]+.0517)/(math.pow(a[i][j]+.2084,2)*(1+math.pow((b[i][j]+.0517)/(a[i][j]+.2084),2)))+(b[i][j]-.0271)/(math.pow(a[i][j]+.2084,2)*(1+math.pow((b[i][j]-.0271)/(a[i][j]+.2084),2)))+(b[i][j]+.0583)/(math.pow(a[i][j]+.525,2)*(1+math.pow((b[i][j]+.0583)/(a[i][j]+.525),2)))+(b[i][j]-.0228)/(math.pow(a[i][j]+.525,2)*(1+math.pow((b[i][j]-.0228)/(a[i][j]+.525),2)))+(b[i][j]+.0242)/(math.pow(a[i][j]+.8417,2)*(1+math.pow((b[i][j]+.0242)/(a[i][j]+.8417),2)))+(b[i][j]-.0087)/(math.pow(a[i][j]+.8417,2)*(1+math.pow((b[i][j]+.0087)/(a[i][j]+.8417),2))))
        u[i][j] = vinf+(vfr/(2*math.pi))*math.pow((b[i][j]+.0047)/(a[i][j]+.05)+(b[i][j]+.0517)/(a[i][j]+.2084)+(b[i][j]-.0271)/(a[i][j]+.2084)+(b[i][j]+.0583)/(a[i][j]+.525)+(b[i][j]-.0228)/(a[i][j]+.525)+(b[i][j]+.0242)/(a[i][j]+.8417)+(b[i][j]-.0087)/(a[i][j]+.8417),-2)*(1/((a[i][j]+.05)*(1+math.pow((b[i][j]+.0047)/(a[i][j]+.05),2)))+1/((a[i][j]+.2084)*(1+math.pow((b[i][j]+.0517)/(a[i][j]+.2084),2)))+1/((a[i][j]+.2084)*(1+math.pow((b[i][j]-.0271)/(a[i][j]+.2084),2)))+1/((a[i][j]+.525)*(1+math.pow((b[i][j]+.0583)/(a[i][j]+.525),2)))+1/((a[i][j]+.525)*(1+math.pow((b[i][j]-.0228)/(a[i][j]+.525),2)))+1/(a[i][j]+.8417)*(1+math.pow((b[i][j]+.0242)/(a[i][j]+.8417),2)))+1/((a[i][j]+.8417)*(1+math.pow((b[i][j]+.0087)/(a[i][j]+.8417),2))))
        p[i][j] = 1-math.pow(math.sqrt(math.pow(u[i][j],2)+math.pow(v[i][j],2)),2)

file = open("data.dat","w")
file.write('title = "Driven Cavity"\r\n')
file.write('variables = "x", "y", "u", "v", "p"\r\n')
file.write('ZONE T="All"\r\n')
file.write(' I={}'.format(len(x)))
file.write(' J={}'.format(len(x)))
file.write(' K=1')
file.write(' ZONETYPE=Ordered\r\n')
file.write(' DATAPACKING=POINT\r\n')

for i in range (0,len(x)):
    for j in range (0,len(y)):
        file.write('{}\t'.format(a[i][j]))
        file.write('{}\t'.format(b[i][j]))
        file.write('{}\t'.format(u[i][j]))
        file.write('{}\t'.format(v[i][j]))
        file.write('{}\t\n'.format(p[i][j]))

file.close()

错误表明它位于u [i] [j] =行的末尾 它可能是一个括号或我无法看到的东西。 从昨晚5点开始,我一直在努力,现在是第二天下午3点40分。

3 个答案:

答案 0 :(得分:2)

行尾的

额外),用以下内容替换该行:

u[i][j] = vinf+(vfr/(2*math.pi))*math.pow((b[i][j]+.0047)/(a[i][j]+.05)+(b[i][j]+.0517)/(a[i][j]+.2084)+(b[i][j]-.0271)/(a[i][j]+.2084)+(b[i][j]+.0583)/(a[i][j]+.525)+(b[i][j]-.0228)/(a[i][j]+.525)+(b[i][j]+.0242)/(a[i][j]+.8417)+(b[i][j]-.0087)/(a[i][j]+.8417),-2)*(1/((a[i][j]+.05)*(1+math.pow((b[i][j]+.0047)/(a[i][j]+.05),2)))+1/((a[i][j]+.2084)*(1+math.pow((b[i][j]+.0517)/(a[i][j]+.2084),2)))+1/((a[i][j]+.2084)*(1+math.pow((b[i][j]-.0271)/(a[i][j]+.2084),2)))+1/((a[i][j]+.525)*(1+math.pow((b[i][j]+.0583)/(a[i][j]+.525),2)))+1/((a[i][j]+.525)*(1+math.pow((b[i][j]-.0228)/(a[i][j]+.525),2)))+1/(a[i][j]+.8417)*(1+math.pow((b[i][j]+.0242)/(a[i][j]+.8417),2)))+1/((a[i][j]+.8417)*(1+math.pow((b[i][j]+.0087)/(a[i][j]+.8417),2)))

答案 1 :(得分:1)

将该行分成几个较小的操作,以便隔离错误。尝试从u[i][j] = 0开始,然后连续添加整行的小部分,直到错误发生,然后你就会知道在哪里寻找它。

答案 2 :(得分:1)

将其粘贴到vim并突出显示最后一个字符表明,至少还有一个额外的关闭。但是,我建议将其分成若干行,以便可以手动调试。出于可读性原因,任何语言中的长线都不是一个好主意。