Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting an invalid syntax error in Python REPL right after IF statement?

I think this is perfectly valid.

if False:
    print(1)
print(2)

However, it gives me an invalid syntax error in Python REPL.

Why is it?

enter image description here

On Python 3.6.5 (x64), Windows 10 RS4

like image 875
Naetmul Avatar asked Jun 18 '18 02:06

Naetmul


1 Answers

As pointed out by user2357112, this behaviour is explained in https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming,

The body of the loop is indented: indentation is Python’s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input for Python with a text editor; all decent text editors have an auto-indent facility. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount.

like image 168
thefourtheye Avatar answered Nov 08 '22 19:11

thefourtheye