为什么我在具有完全有效语法的行中收到“SyntaxError:无效语法”?

2024-04-04

这是代码行:

guess = Pmin+(Pmax-Pmin)*((1-w**2)*fi1+(w**2)*fi2)

Pmin, Pmax, w, fi1 and fi2此时都已经被赋予了有限值,那么为什么会出现错误呢?

当我从代码中删除该行时,同样的错误出现在下一行代码中,同样没有明显的原因。

def Psat(self, T):
    pop= self.getPborder(T)
    boolean=int(pop[0])
   
    P1=pop[1]
    P2=pop[2]
    if boolean:
        Pmin = float(min([P1, P2]))
        Pmax = float(max([P1, P2]))
        Tr=T/self.typeMolecule.Tc
        w=0.5*(1+scipy.tanh((10**5)*(Tr-0.6)))
        fi1=0.5*(1-scipy.tanh(8*((Tr**0.4)-1)))
        fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494

        guess = Pmin+(Pmax-Pmin)*((1-w**2)*fi1+(w**2)*fi2)   # error here
    
        solution = scipy.optimize.newton(funcPsat,guess, args=(T,self))

For earlier versions of Python(1), an error may reported on a line that appears to be correct. In that case, you should try commenting out the line where the error appears to be. If the error moves to the next line, there are two possibilities:

  • Either both线路有问题(第二条线路被第一条线路隐藏);或者
  • The previous线路有问题,正在继续处理。

后者是更倾向于,特别是如果注释掉新的有问题的行会导致错误再次移动。

例如,考虑如下代码,另存为prog.py:

xyzzy = (1 +
plugh = 7

Python 3.8.10 将在第 2 行报告错误,即使问题显然是由第 1 行引起的:

pax> python3.8 prog.py
  File "prog.py", line 2
    plugh = 7
          ^
SyntaxError: invalid syntax

您问题中的代码也有类似的问题:报告错误的前一行代码的括号不平衡。

注释以使其更清楚:

# open parentheses: 1  2             3
#                   v  v             v
fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494
#                               ^             ^
# close parentheses:            1             2

确实没有一个general解决方案 - 需要分析代码并明白了,为了确定how括号应该改变。


(1) For what it's worth, the new PEG parser introduced in Python 3.9 paved the way for much improved error messages (gradually improving from 3.10 thru 3.12). This includes correctly identifying in the source code where the error is:

pax> python3 prog.py
  File "prog.py", line 1
    xyzzy = (1 +
            ^
SyntaxError: '(' was never closed
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我在具有完全有效语法的行中收到“SyntaxError:无效语法”? 的相关文章

随机推荐