-5

任何人都可以在这个 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。

4

3 回答 3

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)))
于 2012-05-29T19:56:14.830 回答
1

将队列分成几个较小的操作,以便隔离错误。u[i][j] = 0尝试从整行的小片段开始并依次添加,直到错误发生,然后您就会知道在哪里查找它。

于 2012-05-29T19:46:51.120 回答
1

将它粘贴到vim并突出显示最后一个字符表明,至少,有一个额外的接近的括号。但是,我建议将其分成多行,以便可以手动调试它。出于可读性的原因,在任何语言中,这么长的行都不是一个好主意。

于 2012-05-29T19:49:31.640 回答