-1

I have a bizarre syntax error with Python and IDLE 3.4 I am reworking some old code. Large amounts of it are commented out with ''' Until yesterday, all was fine in as much as I could work on debugging the code. Then I made a small change to the code, and got a syntax error in some old code that had been OK syntax-wise the previous 10 runs.

The eli in a elif keyword was apparently in error. I deleted and retyped the elif. No change.

It was not the code I was not working on. So I # commented the line out. Now the el in a elif keyword was apparently in error. Note this is now within a comment!

I blocked out the whole text with ''' Now the 3rd ' in the ''' had become the syntax error.

I suspected an illegal character that was not being displayed. I copied the whole program text to an editor. Then copied it again to a new instance of IDLE/Python.

Same problem again.

Today I restarted the whole process. Apart from moving the position of the syntax error (currently it is in the middle of some empty text I created by adding lots of LFs) the syntax error remains, quite unrelated to the program text.

It looks to me as if the error is appearing at a fixed point in the program text.

Any ideas?

Edit - I have added the code. The syntax error has moved again as I modify the text:

    if   VerificationType == 4 :  # I&E
         VerifTypecount4 += 1
         if AD : VT4defcount += 1
         elif Day14Missed : VT4d14count += 1
    elif VerificationType == 3 :
         VerifTypecount3 += 1
         if AD : VT3defcount += 1
         elif Day14Missed : VT3d14count += 1
    elif VerificationType == 2 :
         VerifTypecount2 += 1
         if AD : VT2defcount += 1
         elif Day14Missed : VT2d14count += 1 
    else :
         VerifTypecount1 += 1  # Catch all
         if AD : VT1defcount += 1
         elif Day14Missed : VT1d14count += 1
6
  • Thanks for the quick reply. But what code should I post? The syntax error appears to be positional (e.g. it could be the 5000th character in the input stream).
    – pe9298
    Mar 29, 2015 at 15:08
  • Here is the surrounding block of code (hard to understand this fragment): if AD : VT3defcount += 1 elif Day14Missed : VT3d14count += 1 elif VerificationType == 2 : VerifTypecount2 += 1 if AD : VT2defcount += 1 ''' # ###################elif Day14Missed : VT2d14count += 1 ????????????????????????????????????????????????? ''' else : VerifTypecount1 += 1 # Catch all if AD : VT1defcount += 1 elif Day14Missed : VT1d14count += 1
    – pe9298
    Mar 29, 2015 at 15:10
  • My guess is you didn't code a triple quote properly. Mar 29, 2015 at 15:17
  • Try running it from the command-line instead of IDLE, you might get better diagnostics. In particular it could be that IDLE is running a different file to the one you are looking at.
    – cdarke
    Mar 29, 2015 at 16:37
  • Note that I can shift the apparent position of the syntax error by adding or removing characters in the preceding code. The error is attached to a fixed character position in the code, but not to the actual code itself.
    – pe9298
    Mar 29, 2015 at 16:49

2 Answers 2

1

The code you've inherited is really badly formatted. (And looking at the variable names, it could do with some refactoring as well, like e.g. using dictionaries.)

Use checkers like pep8 or flake8 on the code to find and help you fix problems like these. Your code should generally not produce any errors when you run it through of these checkers.

You can even use autopep8 to fix formatting errors for you.

2
  • I ran pep8 on the program. It threw up a lot of comments, but no comments at all on the block of code giving the error. It is my belief that this is NOT a syntax error, but something else. But I am stuck on what it can be.
    – pe9298
    Mar 29, 2015 at 20:56
  • @pe9298 Can you post the traceback that Python gives when you get an error? Otherwise it's just a guessing game. And it would be a good idea to fix all pep8 errors. Mar 29, 2015 at 21:38
0

The answer, stupidly, was a missing closing bracket in a print statement about 100 lines earlier!

Somehow the python syntax parser must have got confused with the missing bracket and some of the following ''' being used to comment out unwanted old code. This is why sometimes the error was being placed within a commented out section.

Alternatively, maybe the parser has a maximum length for the distance between pairs of brackets, and after this maximum was exceeded, it posted a syntax error.

Unfortunately, both IDLE and GEDIT were ignoring the missing bracket and correctly seeing the ''' to blank out the unwanted code, so I was mislead in looking for an error close to the place where the syntax error was marked.

Thanks to everyone for you comments.

2
  • This is (another) excellent reason not to abuse multi-line strings as comments. Mar 31, 2015 at 13:23
  • IDLE passes your code to python to compile or report SyntaxErrors. Compile marks the point where it discovers that something is wrong. Occasionally, this is far after the mistake. IDLE does try to help user see errors while typing. Coloring strings is one things. Finding the opening ([{ that matches a closing )]} and highligthing everything in between is another. In Python 3, print is a function with ( and ). It is up to users to notice when a ) that they think closes a function call does not highlight back to the opening (. Apr 17, 2017 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.