Pearl Philip Pearl Philip - 1 year ago 77
Python Question

'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


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)

if boolean:
Pmin = float(min([P1, P2]))
Pmax = float(max([P1, P2]))

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

solution = scipy.optimize.newton(funcPsat,guess, args=(T,self))

Answer Source

If you remove the line where the error appears to be and 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.

My bets are on the second option (even more so if you remove another line and it moves again).

For example, the following Python program

xyzzy = (1 +
plugh = 7

generates the error:

  File "", line 2
    plugh = 7
SyntaxError: invalid syntax

despite the problem clearly being on line 1.

When posting code problems, it's better to give context, such as the five lines immediately before and after where you think the error is. You should also post verbatim the error message and any other useful information like a stack trace.

And, now that you have posted the code, it appears I was right. The parentheses in the line before where you get your error, is unmatched, very similar to the example I posted:

# open parentheses: 1  2             3
#close parentheses:             1             2

Depending on what you're trying to achieve, the solution may be as simple as 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 it's to do with molecular biology (assuming PSAT is the enzyme, and the use of the typeMolecule identifier) - I seem to recall failing Biology consistently in my youth :-)