Python’s syntax error are not always perfect in the place they point to. You should also always check the previous lines. Here the syntax highlighting should give you a hint: Else is not colored as a keyword. This is because the correct keyword is else, upper vs lower case is important in python. Answer from MegaIng on discuss.python.org
🌐
Real Python
realpython.com › invalid-syntax-python
Invalid Syntax in Python: Common Reasons for SyntaxError – Real Python
March 18, 2026 - The interpreter will find any invalid syntax in Python during this first stage of program execution, also known as the parsing stage. If the interpreter can’t parse your Python code successfully, then this means that you used invalid syntax somewhere in your code.
Discussions

What exactly is "invalid syntax" and why do I keep getting it in Python? - Stack Overflow
Invalid syntax simply means that the code you have written cannot be interpreted as valid instructions for python. More on stackoverflow.com
🌐 stackoverflow.com
Why do I get "SyntaxError: invalid syntax" in a line with ...
This is the line of code: guess = Pmin+(Pmax-Pmin)*((1-w**2)*fi1+(w**2)*fi2) Pmin, Pmax, w, fi1 and fi2 have all been assigned finite values at this point, so why is there an error? When I remove ... More on stackoverflow.com
🌐 stackoverflow.com
How to deal with the error 'invalid syntax'?
How to deal with the error 'invalid syntax' · Invalid function definitions or calls. For example, missing a colon in a function definition or missing parentheses in a function call · Invalid variable declarations. For example, starting the variable name with a number or using invalid characters More on root-forum.cern.ch
🌐 root-forum.cern.ch
3
0
August 30, 2024
"Invalid Syntax" error despite completely valid syntax
I get an invalid syntax error when I try to subtract an input variable from another variable, despite the syntax being completely valid. I’ve tried to use different named variables. Here is my code: init_bal = 1000.00 bal = init_bal name = raw_input("What is the name on your account?: ") ... More on discuss.python.org
🌐 discuss.python.org
8
0
May 15, 2024
🌐
Python.org
discuss.python.org › python help
Syntax error I can't understand - Python Help - Discussions on Python.org
August 5, 2024 - Hello. I am learning Python, and when I try to run this I get a syntax error: invalid syntax error, and I have no idea why. Any help is massively appreciated! Thanks!
🌐
OneUptime
oneuptime.com › home › blog › how to fix 'syntaxerror: invalid syntax' in python
How to Fix 'SyntaxError: invalid syntax' in Python
January 26, 2026 - "SyntaxError: invalid syntax" is one of the most common errors Python developers encounter. It means Python cannot parse your code because it violates the language's grammar rules.
Find elsewhere
Top answer
1 of 4
109

For earlier versions of Python(1), an error may be 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
2 of 4
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 ).

🌐
CERN
root-forum.cern.ch › t › how-to-deal-with-the-error-invalid-syntax › 60655
How to deal with the error 'invalid syntax'? - ROOT - ROOT Forum
August 30, 2024 - How to deal with the error 'invalid syntax' · Invalid function definitions or calls. For example, missing a colon in a function definition or missing parentheses in a function call · Invalid variable declarations.
🌐
Stackify
stackify.com › syntax-errors-in-python-a-complete-explanation
Syntax Errors in Python: A Complete Explanation - Stackify
July 24, 2024 - Syntax errors in Python occur when the interpreter encounters code that the interpreter does not understand. These errors prevent your code from executing.
🌐
Python.org
discuss.python.org › python help
"Invalid Syntax" error despite completely valid syntax - Python Help - Discussions on Python.org
May 15, 2024 - I get an invalid syntax error when I try to subtract an input variable from another variable, despite the syntax being completely valid. I’ve tried to use different named variables. Here is my code: init_bal = 1000.00 bal = init_bal name = raw_input("What is the name on your account?: ") invalid = False transact = True print "Hello,", name+"!" def banking(): global transact while transact == True: global transact global invalid global bal global name print "Your balance is $"+str(ro...
🌐
Quora
quora.com › Why-do-I-keep-getting-and-invalid-syntax-error-when-I-use-+-operator-in-Python
Why do I keep getting and 'invalid syntax' error when I use += operator in Python? - Quora
Answer (1 of 2): Because += is also an assignment operator. Assignment operators don't make expressions. Only expressions' results can be returned. Think of it like this: > i + 1 is an expression. It returns a value one increment of i. Similarly, if f is a function that returns something, the...
🌐
Python Morsels
pythonmorsels.com › syntaxerror-invalid-syntax
SyntaxError: invalid syntax - Python Morsels
October 31, 2022 - This is Python's way of saying "I don't understand you". Python knows that what you've typed isn't valid Python code but it's not sure what advice to give you. When you're lucky, your SyntaxError will have some helpful advice in it:
🌐
Aguaclara
aguaclara.github.io › aguaclara_tutorial › python › python-common-errors.html
Common Errors in Python — AguaClara Tutorial v0.1.0 documentation
A syntax error is an error in the syntax of a command, much like a typo. Python will refuse to even attempt compiling (preparing to run) code that contains syntax errors.
🌐
Rollbar
rollbar.com › home › how to fix invalid syntaxerror in python
How to Fix Invalid SyntaxError in Python | Rollbar
The Python SyntaxError occurs when the interpreter encounters invalid syntax. Learn how to fix common causes with examples and debugging tips.
Published: June 30, 2026
🌐
Quora
quora.com › What-does-Invalid-Syntax-in-IDLE-Python-programming-language-mean
What does 'Invalid Syntax' in IDLE (Python programming language) mean? - Quora
Answer (1 of 2): That the text you typed is not a valid Python program, or at least not valid for the version you’re using: e.g. in Python 2 this is a valid program - print “hello” - while in Python 3 it is not, there it has to be - print(“hello”)
🌐
Treeage
treeage.com › help › Content › 98-Python › 5-Python-Syntax-Errors.htm
Python Syntax Errors
The example in the Special Features tutorial example model, Python Example – SyntaxError.trex, has invalid Python code within the variable definition for function PythonFunctionSyntaxError. Specifically, the internal Python variable missing_input is not declared within the function or passed as an argument to the function.
🌐
Medium
medium.com › python-for-engineers › common-code-errors-in-python-6e388cf882e0
Common Code Errors in Python. Syntax Errors | by Shaloo Mathew | LearnPython | Medium
January 26, 2025 - The syntax error simply means that the Python parser is unable to understand a line of code. If you attempted to run the following code before correcting it: print("My favorite quote is "To be or not to be."") You may have seen something like ...
🌐
Quora
quora.com › Why-is-Python-saying-my-syntax-is-invalid-I’m-trying-to-open-read-a-file-and-no-matter-what-I-try-it-doesn’t-work
Why is Python saying my syntax is invalid? I’m trying to open/read a file, and no matter what I try it doesn’t work. - Quora
Answer (1 of 13): The first thing is that your filename needs to be in quotes: [code]words_file = open("/Users/Sammi/Desktop/Hw3/words.txt","r") [/code]The words_file variable is just a reference to the file, not the contents to the file. One way to load the words from the file would be: [code]...
🌐
Facebook
facebook.com › groups › python › posts › 1142165749957321
What is invalid syntax in Python?
Popular groups · Find communities for you · Over 1 billion people across the globe are using Facebook Groups to explore their favorite topics · Log in · Categories · Science & tech · Travel · Animals · Sports & fitness · Entertainment
🌐
GitHub
github.com › trufflesecurity › trufflehog
GitHub - trufflesecurity/trufflehog: Find, verify, and analyze leaked credentials · GitHub
August 8, 2026 - unverified: Credential detected but not confirmed valid (may be invalid, expired, or verification disabled)
Author: trufflesecurity