Python indentation semantics

荒凉一梦 提交于 2019-12-12 06:49:09

问题


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

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'

Will print 'program done' in this case be part of the block code associated with else?


回答1:


From Python Docs:

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.

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



来源:https://stackoverflow.com/questions/34383700/python-indentation-semantics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!