Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Syntax Error: invalid syntax' for no apparent reason

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

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)) 
like image 936
Pearl Philip Avatar asked Jun 16 '14 05:06

Pearl Philip


People also ask

How do I fix SyntaxError invalid syntax?

You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.

Why does Python keep saying invalid syntax?

Syntax errors are produced by Python when it is translating the source code into byte code. They usually indicate that there is something wrong with the syntax of the program. Example: Omitting the colon at the end of a def statement yields the somewhat redundant message SyntaxError: invalid syntax.

How do you find the syntax error in Python?

Python error checker tool allows to find syntax errors (lint). You can test your Python code online directly in your browser. If a syntax error is detected, then the line in error is highlighted, and it jumps to it to save time (no need to search the line).


1 Answers

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 both lines have a problem or the previous line has a problem which is being carried forward. The most likely case 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:

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.


In your particular case, that is the problem. The parentheses in the line before your 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 may be as simple as just adding another closing parenthesis at the end, to close off the sqrt function.

I can't say for certain since 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 typeMolecule identifier) it's to do with molecular biology - I seem to recall failing Biology consistently in my youth :-)

like image 188
paxdiablo Avatar answered Sep 28 '22 22:09

paxdiablo