为什么我在 IF 语句后立即在 Python REPL 中收到无效语法错误?

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

我认为这是完全正确的。

if False:
    print(1)
print(2)

但是,它在 Python REPL 中给我一个无效的语法错误。

这是为什么?

在 Python 3.6.5 (x64) 上,Windows 10 RS4

正如 user2357112 所指出的,此行为在 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.

REPL 一次只能读取和评估一个语句。 您一次输入了两个语句。

这是可能的,因为 REPL 无法决定第三行是继续 if 构造还是开始一个全新的语句。它必须假定前者完全允许缩进块。

在开始新的语句之前,您必须向 REPL 明确表示您之前的语句已完成。

错误的版本似乎是最有可能的,因为在错误中它显示打印。在旧的 python 版本中,print 被用作 print"ok",我看到你的操作系统是 windows 所以你可以直接从 [= 下载 python3 14=]https://python.org/祝你有个愉快的一天!