Are you sure it's a syntax error?

If it does not contain the word "SyntaxError" it must be a run time error.

Line numbers

The error message will give you a line number or highlight a point on your code. You should debug the line based on what you are doing on that line: for example, if the highlighted line is an assignment, make sure you have the right syntax for an assignment. Be careful - sometimes a mistake on one line can cause an error on the line before or the line after or even, in some cases, right at the end of the file.

Common Errors

Some very common syntax errors are:

Sometimes, misspelling a Python keyword can cause syntax errors.

Don't forget that Python is case sensitive! This includes keywords (like "def", "for" and "while") as well as variable names. There are some examples of misspelt variable names here.

In the following example, the "while" keyword has been incorrectly spelt with a capital "w". Note that in this case, the error message is a little
misleading since the arrow points to the wrong part of the code.

Missing quotation marks: you need to use "string", not "string.

In this example, the literal "world" is missing a closing quotation mark. If you read the error carefully (in which 'EOL' means 'End of Line'), you will see it is saying that it found the end of the line while scanning through a string - and this is a problem because we need the closing quote on the same line. Always try to get information from the error message.

Simplify

Your code may be too complicated to tell which part of it is causing the error. In this case, try removing some of the complexity until it does work. Once the error has disappeared, try adding the complex code you need a piece at a time until you can identify the source of the problem. Maybe one of the pieces you have added has not been expressed in a way Python can understand. Comparing to examples may help.

Indentation

Python uses indentation to show where each block of code begins and ends. Indentation can be achieved with spaces or tabs, but a mixture will almost always lead to confusion and errors. Since a tab and 4 spaces look the same on the screen, two pieces of code can look identical to you, but to Python they look entirely different. This is the cause of many non-obvious errors. If your code looks perfect but is still giving syntax errors, check that your indentation is correct.

Below are two causes of indentation errors, one in a function and one in a loop. In both cases, spaces have been mixed with tabs.