为什么我在 IF 语句之后立即在 Python REPL 中收到无效的语法错误? [英] Why am I getting an invalid syntax error in Python REPL right after IF statement?

查看:51
本文介绍了为什么我在 IF 语句之后立即在 Python REPL 中收到无效的语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是完全正确的.

I think this is perfectly valid.

if False:
    print(1)
print(2)

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

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

为什么会这样?

在 Python 3.6.5 (x64)、Windows 10 RS4 上

On Python 3.6.5 (x64), Windows 10 RS4

推荐答案

正如 user2357112 所指出的,这种行为在 https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming,

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

循环体是缩进的:缩进是 Python 对语句进行分组的方式.在交互式提示下,您必须为每个缩进的行键入一个制表符或空格.在实践中,您将使用文本编辑器为 Python 准备更复杂的输入;所有体面的文本编辑器都有自动缩进功能.当复合语句以交互方式输入时,它后面必须跟一个空行以表示完成(因为解析器无法猜测您何时输入了最后一行).请注意,基本块中的每一行都必须缩进相同的数量.

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.

这篇关于为什么我在 IF 语句之后立即在 Python REPL 中收到无效的语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆