Invalid Syntax

Steven D'Aprano steve+python at pearwood.info
Tue Aug 9 20:59:47 EDT 2016


On Wed, 10 Aug 2016 06:20 am, Ltc Hotspot wrote:

> Hi, Everyone:
> 
> What is the source of the following,
> 'error message: SyntaxError: invalid syntax (<string>, line 2)'
> 
> v. Python 3.3
> 
> Code reads:
> 
> x=1
> if x==1
>     # indent 4 spaces
>     print "x = 1"
> 
> Hal

The error message tells you exactly which line the error is on: line 2. Have
you done a tutorial to learn how to write Python code? If you haven't, you
should:

https://docs.python.org/3/tutorial/


The error is that all statements that introduce a new indented block require
a colon:

    class C:
        def func():
              if condition:
                   for x in sequence:
                        # etc.


Also, print() is a function, you need round brackets (parentheses):

    print("x = 1")




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list