Skip to content
Advertisement

why this way of executing python isn’t working

python -c 'for i in range(10): print i'

works, and

python -c 'a=3;'

works, but

python -c 'a=3;for i in range(10): print i'

gave an error

  File "<string>", line 1
    a=3;for i in range(10): print i
          ^
SyntaxError: invalid syntax

Could anyone help point out why? Thank you.

Advertisement

Answer

; can only be used to separate “simple” statements, not compound statements.

https://docs.python.org/3/reference/compound_stmts.html

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement