0

My code always gives invalid syntax errors in different lines. I mean it is giving an error at line 143. Then when I clear that line. It gives the same error at another line. Even I get invalid syntax error at a line like:

print("======================================================================")

How can this be possible? Any ideas? Thank you ^^

uhuuyouneverknow
  • 403
  • 2
  • 6
  • 14
  • 1
    Please, post the exact error message and the lines where you get them – Michael Kruglos Nov 04 '15 at 21:35
  • 1
    Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Morgan Thrapp Nov 04 '15 at 21:41
  • https://stackoverflow.com/questions/19472645/syntaxerror-invalid-syntax-on-valid-statement – user202729 Aug 15 '19 at 07:59

1 Answers1

5

It's hard to know without seeing the whole code. However, be aware that python has implied line continuation with parentheses and other brackets.

This can mean that errors can be reported on a different to that on which the true mistake has been made.

For example:

a = (3 * 4) + (3 * 2
print "Hello"

...gives the error:

 File "<ipython-input-1-53e17eda21df>", line 2
    print "Hello"
        ^
SyntaxError: invalid syntax

What is happening is that Python expects the first line to continue, e.g like:

a = (3 * 4) + (3 * 2
    + 100)

...which is completely valid syntax. However, it finds print "Hello" instead, which isn't a valid continuation of the statement inside the brackets.

Community
  • 1
  • 1
Bill Cheatham
  • 11,396
  • 17
  • 69
  • 104
  • When I read your answer, I was expecting it to end with something like "in other words, you're missing a parenthesis on a preceding line". While the answer accurately describes the situation, it may be a bit vague for the type of person who would ask this type of question. You might want to strengthen the answer by giving some practical advice, such as looking for a missing parenthesis, bracket or quote on a previous line. – Bryan Oakley Nov 04 '15 at 22:14