Python 无明显原因的“语法错误:无效语法”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24237111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:16:58  来源:igfitidea点击:

'Syntax Error: invalid syntax' for no apparent reason

pythonsyntax-errorspyder

提问by Pearl Philip

I've been trying to get a fix and can't find why the error keeps appearing. Pmin,Pmax,w,fi1 and fi2 have all been assigned finite values

我一直在尝试修复,但找不到错误不断出现的原因。Pmin、Pmax、w、fi1 和 fi2 都已分配有限值

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

When i remove this line from the code, the same error appears at the next line of code, again for no reason I can think of

当我从代码中删除这一行时,同样的错误出现在下一行代码中,我再次无缘无故地想到

Edit: Here is the chunk of code I was referring to:

编辑:这是我所指的代码块:

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))

采纳答案by paxdiablo

For problems where it seems to be an error on a line you think is correct, you can often remove/comment the line where the error appears to be and, if the error moves to the next line, there are two possibilities.

对于在您认为正确的行上似乎是错误的问题,您通常可以删除/注释出现错误的行,如果错误移动到下一行,则有两种可能性。

Either bothlines have a problem or the previousline has a problem which is being carried forward. The most likelycase is the second option (even more so if you remove another line and it moves again).

要么两条线都有问题,要么一条线有问题正在推进。最可能的情况是第二个选项(如果您删除另一条线并再次移动,则更是如此)。

For example, the following Python program twisty_passages.py:

例如,以下 Python 程序twisty_passages.py

xyzzy = (1 +
plugh = 7

generates the error:

产生错误:

  File "twisty_passages.py", line 2
    plugh = 7
          ^
SyntaxError: invalid syntax

despite the problem clearly being on line 1.

尽管问题显然在第 1 行。



In your particular case, that isthe problem. The parentheses in the line beforeyour error line is unmatched, as per the following snippet:

在您的特定情况下,这就是问题所在。根据以下代码段,错误行之前行中的括号不匹配:

# 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

Depending on what you're trying to achieve, the solution maybe as simple as just adding another closing parenthesis at the end, to close off the sqrtfunction.

根据您要实现的目标,解决方案可能很简单,只需在末尾添加另一个右括号即可关闭sqrt函数。

I can't say for certainsince I don't recognise the expression off the top of my head. Hardly surprising if (assuming PSAT is the enzyme, and the use of the typeMoleculeidentifier) it's to do with molecular biology - I seem to recall failing Biology consistently in my youth :-)

我不能肯定地说,因为我不认识我头顶上的表情。如果(假设 PSAT 是酶,并且typeMolecule标识符的使用)与分子生物学有关,那就不足为奇了- 我似乎记得我年轻时一直失败的生物学:-)

回答by aaron newland

You're missing a close paren in this line:

您在此行中缺少一个关闭括号:

fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494

There are three ( and only two ).
I hope This will help you.

有三个(而且只有两个)。
我希望这能帮到您。

回答by jbyler

I encountered a similar problem, with a syntax error that I knew should not be a syntax error. In my case it turned out that a Python 2 interpreter was trying to run Python 3 code, or vice versa; I think that my shell had a PYTHONPATH with a mixture of Python 2 and Python 3.

我遇到了类似的问题,语法错误我知道不应该是语法错误。就我而言,结果是 Python 2 解释器试图运行 Python 3 代码,反之亦然;我认为我的 shell 有一个混合了 Python 2 和 Python 3 的 PYTHONPATH。

回答by Ryan w

If you are running the program with python, try running it with python3.

如果您使用 python 运行该程序,请尝试使用 python3 运行它。