JavaScript
x
2
1
python -c 'for i in range(10): print i'
2
works, and
JavaScript
1
2
1
python -c 'a=3;'
2
works, but
JavaScript
1
2
1
python -c 'a=3;for i in range(10): print i'
2
gave an error
JavaScript
1
5
1
File "<string>", line 1
2
a=3;for i in range(10): print i
3
^
4
SyntaxError: invalid syntax
5
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
JavaScript
1
14
14
1
compound_stmt ::= if_stmt
2
| while_stmt
3
| for_stmt
4
| try_stmt
5
| with_stmt
6
| funcdef
7
| classdef
8
| async_with_stmt
9
| async_for_stmt
10
| async_funcdef
11
suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
12
statement ::= stmt_list NEWLINE | compound_stmt
13
stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
14