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