Syntax Error

The program doesn't run: it has a syntax error

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.

1    counter=0
2    While counter < 5:
3        print "hello"
4        counter = counter + 1

Traceback (most recent call last):
   File "./tester.py", line 2
    While counter < 5:
                ^
SyntaxError: invalid syntax

Missing off a colon from the end of an if or while line.

In the following example, a colon is missing from the end of the if conditional line.

1    counter = 0
2    if counter == 4
3        print "counter is 4"

  File "tester.py", line 2
    if counter == 4
                  ^
SyntaxError: invalid syntax

The wrong number of brackets in function calls or expressions.

In the following example, a bracket is missing to complete the expression at the end of the line. Note that the error message points to the line below where the error actually occurs.

1    x = 3
2    y = ((x + 3) * (x + 4)
3    print y

  File "tester.py", line 3
    print y
        ^
SyntaxError: invalid syntax

In this example, an opening bracket is missing from the raw_input function call.

1    name = raw_input)
2    print name

  File "tester.py", line 1
    name = raw_input)
                    ^
SyntaxError: invalid syntax

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.

1    greeting = "hello" + " world
2    print greeting

  File "tester.py", line 1
    greeting = "hello" + " world
                               ^
SyntaxError: EOL while scanning single-quoted string

Incorrect indentation

Sometimes, Python will report [indentation problems] as syntax errors.

1        x = 0
2    y = 1

  File "tester.py", line 1
    x = 0
    ^
SyntaxError: invalid syntax

Examples

If your code is not working, you should try comparing it to code that does work. Here are some possibilities for working code:

  1. Lecture notes and sample code provided on the course.
  2. Code you have written yourself in previous exercises.
  3. Code from the web. Try these: assignments, if statements, for statements, function definitions.
  4. There's also a great page here that might help. Try it :)

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.

1    def myfunc():
2        print "hello"
3        print "world"
4    myfunc()

  File "tester.py", line 3
    print "world"
                ^
IndentationError: unindent does not match any outer indentation level
1    counter = 0
2    while counter < 5:
3        print "hello"
4        counter = counter + 1

  File "tester.py", line 4
    counter = counter + 1
                        ^
IndentationError: unindent does not match any outer indentation level
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License