Python F&Q

发布时间: 更新时间: 总字数:89 阅读时间:1m 作者:IP:上海 网址

Python F&Q 汇总

常见错误

python-if-else-SyntaxError:invalid syntax 问题

if elif and else must immediately follow the end of the if block, or Python will assume that the block has closed without them.

text
if 1:
    pass
           <--- this line must be indented at the same level as the `pass`
else:
    pass

In your code, the interpreter finishes the if block when the indentation, so the elif and the else aren’t associated with it. They are thus being understood as standalone statements, which doesn’t make sense.

Python IDLE 中 else 缩进错误引发 SyntaxError 报错界面 Windows 命令提示符下交互式运行 Python if-else 缩进示例 Python IDLE 交互环境中正确缩进的 if-else 语句执行结果