-6

我需要 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()

别介意只是另一个括号。漫长的一天

4

4 回答 4

4

您错过了)行尾的关闭:

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))
于 2012-05-28T23:23:17.113 回答
1

(math.pow(a[i][j],2)语句之前的马拉松表达式中的这个特定表达式if看起来好像缺少结束语)

IE,

s[i][j] = (vinf*b[i][j])*(1-(math.pow(3,2)/(math.pow(a[i][j],2) + ...

应该

s[i][j] = (vinf*b[i][j])*(1-(math.pow(3,2)/(math.pow(a[i][j],2)) + ...

虽然很难确定表达式的长度和所需的可能分组。

于 2012-05-28T23:26:27.947 回答
1

为避免不匹配的 ()、{}、[]、''、"" 等错误,请使用代码编辑器,在您使用关闭器时指示开启器,最好了解特定语言。Idle 的编辑器针对 Python 执行此操作,Notepad++ 针对多种语言执行此操作,而其他几个编辑器针对各种语言执行此操作。

当你得到一个看起来错误的令人费解的 SyntaxError 时(在指示的位置)以某种方式修改代码并查看指示器是否移动。在您的原始代码中,如果您在 if 之前的行中放置了任何内容,例如 '1==1',则 ^ 将移至该行。那么你就会知道问题出在 if 语句之前。

于 2012-05-29T00:16:12.120 回答
0

与您的问题没有直接关系,但是:您的代码似乎做了很多不必要的工作。我试着精简一下。

from math import log1p, sqrt, pi
NEWLINE = '\r\n'
header = NEWLINE.join([
    'title = "Driven Cavity"',
    'variables = "x", "y", "u", "v", "p", "s"',
    'ZONE T="All"',
    ' I={} J={} K=1 ZONETYPE=Ordered',
    ' DATAPACKING=POINT',
    ''
])

for o in range(3):
    if gam < 1:    fname = "data1.dat"
    elif gam == 1: fname = "data2.dat"
    else:          fname = "data3.dat"

    with open(fname, "w") as outf:
        outf.write(header.format(len(x), len(y)))

        for _x in x:
            for _y in y:
                _xsq = _x**2
                _ysq = _y**2
                sqsq = (_xsq + _ysq)**2
                chunk = gamma*log1p(sqrt(_xsq+_ysq)/3.)/pi

                u = 0.5*vinf*_x*_y*(36.*pi + gamma*(_xsq+_ysq))/(pi*sqsq)
                v = 0.5*vinf*(2. + _ysq*(36.*pi + _y*(_xsq+_ysq))/(pi*sqsq) - 18./(_xsq+_ysq) + chunk)
                p = 1. - (u**2 + v**2)
                s = vinf*_y*(1. - 9./(_xsq+_ysq) + 0.5*chunk)

                outf.write('{}\t{}\t{}\t{}\t{}\t{}{}'.format(_x, _y, u, v, p, s, NEWLINE))

希望这可以帮助。

于 2012-05-29T01:55:36.933 回答