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

标签 python read-eval-print-loop

我认为这是完全正确的。

if False:
    print(1)
print(2)

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

这是为什么?

enter image description here

在 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.

关于python - 为什么我在 IF 语句后立即在 Python REPL 中收到无效语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50901923/

相关文章:

arrays - 如何避免最后打印nil?

python - 如何从 Python(或其他语言)的文本 block 中解析多个日期

python - 使用 nltk 绘制 50 个最不常见的单词

xcode - REPL 中的 `Error creating target Swift AST context: (null)`

Swift REPL : how to import, 加载、评估或需要 .swift 文件?

go - Go 提供 REPL 吗?

python - 将请求中的 JSON 数据转换为 Pandas DataFrame

python - 如何在python中转换关联数组?

python - Tensorflow 中的反向传播——准确性不会随着隐藏层大小的增加而提高

python - 如何检查您使用的 REPL 类型?