58

I have this code:

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

On the marked line of code, guess = Pmin+(Pmax-Pmin)*((1-w**2)*fi1+(w**2)*fi2), I get an error message: SyntaxError: invalid syntax.

Pmin, Pmax, w, fi1 and fi2 have all been assigned at this point, so why is there an error?

When I remove that line from the code, the same error appears at the next line of code, again for no apparent reason.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Pearl Philip
  • 883
  • 2
  • 11
  • 16
  • 3
    The root of the error is probably occuring above the line you posted. Can you post a little more code? – Bryan Jun 16 '14 at 05:33
  • and the full traceback – K DawG Jun 16 '14 at 05:33
  • You probably forgot a parenthesis somewhere before. Impossible to tell exactly without larger context. Please provide the full traceback and code that reproduces the problem. – Bakuriu Jun 16 '14 at 05:37
  • 2
    I found the error, there was a missing bracket in one of the previous lines. Thanks for the help – Pearl Philip Jun 16 '14 at 05:47
  • 1
    Starting with Python 3.10, we get better error messages for this kind of thing. In this case, `SyntaxError: '(' was never closed`, and there's an arrow that points to the opening parenthesis. – wjandrea Mar 03 '23 at 04:53

4 Answers4

104

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 lines have a problem (and the second was hidden by the first); or
  • The previous line has a problem which is being carried forward.

The latter is more likely, especially if commenting out the new offending line causes the error to move again.

For example, consider code like the following, saved as prog.py:

xyzzy = (1 +
plugh = 7

Python 3.8.10 will report an error on line 2, even though the problem is clearly caused by line 1:

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

The code in your question has a similar problem: the code on the previous line to the reported error has unbalanced parentheses.

Annotated to make it clearer:

# 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

There isn't really a general solution for this - the code needs to be analyzed and understood, in order to determine how the parentheses should be altered.


(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
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
9

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
aaron newland
  • 91
  • 1
  • 1
  • My case was categorized as "the previous line has a problem which is being carried forward" according to paxdiablo . What I did to find the error was by commenting line by line upward the code until the Syntax Error disappeared temporary. The last line that I gave comment mark was the one who cause error. – Santosa Sandy Mar 28 '17 at 08:00
3

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.

jbyler
  • 7,200
  • 3
  • 34
  • 42
-3

I noticed that invalid syntax error for no apparent reason can be caused by using space in:

print(f'{something something}')

Python IDLE seems to jump and highlight a part of the first line for some reason (even if the first line happens to be a comment), which is misleading.

michalmonday
  • 441
  • 5
  • 11
  • 3
    f-string-invalid-syntax-in-python-3-5: https://stackoverflow.com/questions/55182209/f-string-invalid-syntax-in-python-3-5 – davesave Jun 24 '21 at 12:50
  • Does Python's error show the wrong line, or is this only an IDLE thing? Either way, this is a different issue than the one OP's experiencing, if you read their code. – wjandrea Mar 03 '23 at 05:14