问题
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