I don't understand why I'm having an error with Python when I use a single-line if
statement after a semicolon (used as a statement separator).
This is ok:
if True: print("it works")
#### it works
But this gives a syntax error:
a=1; if True: print("it should work?")
#### SyntaxError: invalid syntax
I use Python3, with Spyder.
Thanks for any explanation!
Semicolons can only be used to join "small statements", which don't include if
statements. From https://docs.python.org/3/reference/grammar.html:
stmt: simple_stmt | compound_stmt
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
[...]
compound_stmt: if_stmt | [...]