The intended audience for this document is the Fall 2010 ISTA 130 class at the University of Arizona. If other people also find it helpful, great!
Click on the name of the message for an explanation of what may have caused it, and a suggestion or two on how to fix the problem.
The syntax of a language is the set of rules that define what parts of the language can appear in which places. If you insert tab A into slot B, so to speak, you'll create a statement that has invalid syntax. Because there are lots of slots in Python that don't accept most tabs, generating a syntax error is not hard.
Here are some common errors that cause this message:
Indentation of program statements is critical to the readability of code. Most programming languages permit indentation, but don't enforce it. Python enforces it with an iron fist. Whenever you have a situation with code inside of a statement (such as the code that defines the main() function), that 'inside' code must be indented, and must be indented consistently. If you forget to indent, you'll see this error.
Some common causes of this error include:
As mentioned in the "expected an indented block" section, Python not only insists on indentation, it insists on consistent indentation. You are free to choose the number of spaces of indentation to use, but you then need to stick with it. If you indent one line by 4 spaces, but then indent the next by 3 (or 5, or 19, or ...), you'll get this error.
Some common causes of this error include:
Python knows the purposes of certain names (such as names of built-in functions like print). Other names are defined within the program (such as variables). If Python encounters a name that it doesn't recognize, you'll probably get this error.
Some common causes of this error include:
Python variables 'know' the kinds of values they hold, which allows Python to tell you when you're trying to do something strange, such as use the addition operator to combine a number and a string (answer = "Hello" + 1).
A common cause of this error is forgetting to use eval() with input() when asking the user to enter a numeric value from the keyboard.
Do you have a comment on this page? I'd like to hear it; you can email me at mccannl@acm.org.