Python缩进语义 [英] Python indentation semantics

查看:102
本文介绍了Python缩进语义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是有一个非常简单的问题,只需要确认我明白了。

I just have a very simple question, and just need to confirm that I got the point.

假设我们有以下Python代码片段:

Say that we have the following Python code snippet:

x = 0
if x == 5:
    print 'x is equal to 5'
else:
    print 'x is not equal to 5'

print 'program done'

如果我们将语句打印'程序完成'缩进如下:

If we indent the statement print 'program done' as follows:

else:
    print 'x is not equal to 5'
    print 'program done'

打印'程序完成'在这种情况下是与 else

推荐答案

来自 Python文档


前导空格(空格和制表符)逻辑行的开头用于计算th e行的缩进级别,用于确定语句的分组。

Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements.

所以,是的,它将属于 else 阻止。

So, yeah, it will belong to the else block.

这篇关于Python缩进语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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