Interpretation

Steven D'Aprano steve at pearwood.info
Sat Mar 26 23:49:24 EDT 2016


On Sat, 26 Mar 2016 03:33 am, Dennis Ngeno wrote:

> My programs have never combile, they keep telling me , systax error even
> after copy pasting

Dennis, if Python is complaining that you have a SyntaxError, it means you
have written something which the interpreter cannot understand. We cannot
possibly tell what you have done wrong if you don't show us what you wrote,
and what the error message says.


SyntaxError can mean many different errors, for example:


- you have misspelled a keyword (e.g. "dfe" instead of "def", "clas" instead
of "class", "ipmort" instead of "import");


- you tried to use a keyword as a variable (e.g. "class = 23");

- you have not indented a code block correctly, e.g. inconsistent
indentation in a single code block:

def function():
    print(1)  # four spaces indent
        print(2)  # eight spaces! this is inconsistent
  print(3)  # now two spaces!


or forgetting to indent a code block:

def function():
print(1)  # no indentation at all


If you mix spaces and tabs, Python can sometimes get confused about the
indentation, so you should always use *only* spaces or *only* tabs, never
both in the same block.


- you have too many, or too few, brackets and parentheses, or they are in
the wrong place, e.g.:

    def function():
        result = math.sin(1.245  # oops, forgot the closing parenthesis
        return result + 1


Errors with brackets and parentheses can be the most annoying ones, because
sometimes Python cannot tell that there is an error until the line *after*
where the error is. So in this case, it will say that there's a SyntaxError
in the last line, "return result + 1", when the real error is in the
previous line.

SyntaxError tells you that your code is written wrongly and Python doesn't
understand what you mean. You have to look at the code and fix the syntax
problem. If you show us the line of code that Python is complaining about,
and maybe the line before it, we can help.




-- 
Steven




More information about the Python-list mailing list