我需要 python 3.2 if-else 语句的帮助,代码是:
if gam < 1:
file = open("data1.dat","w")
gam = gam2
elif gam == 1:
file = open("data2.dat","w")
gam = gam3
elif gam > 1:
file = open("data3.dat","w")
运行时,如果 gam < 1 如果我删除冒号,我会收到指向冒号的语法错误点,我会收到指向 word 文件的语法错误。
编辑:这里有更多的代码
for o in range (3):
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)):
u[i][j] = (vinf*a[i][j]*b[i][j]*(4*math.pi*math.pow(3,2)+gamma*(math.pow(a[i][j],2)+math.pow(b[i][j],2))))/(2*math.pi*math.pow(math.pow(a[i][j],2)+math.pow(b[i][j],2),2))
v[i][j] = (1/2)*vinf*((math.pow(b[i][j],2)*(4*math.pi*math.pow(3,2)+b[i][j]*(math.pow(a[i][j],2)+math.pow(b[i][j],2))))/(math.pi*math.pow(math.pow(a[i][j],2)+math.pow(b[i][j],2),2))-(2*math.pow(3,2))/(math.pow(a[i][j],2)+math.pow(b[i][j],2))+(gamma*math.log1p(math.sqrt(math.pow(a[i][j],2)+math.pow(b[i][j],2))/3))/math.pi+2)
p[i][j] = 1-(math.pow(u[i][j],2)+math.pow(v[i][j],2))
s[i][j] = (vinf*b[i][j])*(1-(math.pow(3,2)/(math.pow(a[i][j],2)+math.pow(b[i][j],2)))+(gamma/(2*math.pi))*math.log1p(math.sqrt(math.pow(a[i][j],2)+math.pow(b[i][j],2))/3)
if gam < 1:
file = open("data1.dat","w")
gam = gam2
elif gam == 1:
file = open("data2.dat","w")
gam = gam3
elif gam > 1:
file = open("data3.dat","w")
edit2:我似乎在 s 行缺少括号,但现在关闭文件时出现错误
if gam < 1:
file = open("data1.dat","w")
gam = gam2
elif gam == 1:
file = open("data2.dat","w")
gam = gam3
elif gam > 1:
file = open("data3.dat","w")
file.write('title = "Driven Cavity"\r\n')
file.write('variables = "x", "y", "u", "v", "p", "s"\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'.format(p[i][j]))
file.write('{}\t\n'.format(s[i][j])
file.close()
别介意只是另一个括号。漫长的一天