"block"在Python3中是什么意思?
What does "block" mean in Python 3?
我是编程新手,正在尝试向 automatetheboringstuff.com 学习 Python。
在第 2 章的末尾,出现了以下问题。
甚至在我已经通过官方答案之后,我仍然一无所知。
请帮忙!
Q:8. Identify the three blocks in this code:
spam = 0
if spam == 10:
print('eggs')
if spam > 5:
print('bacon')
else:
print('ham')
print('spam')
print('spam')
官方回答:
The three blocks are everything inside the if statement and the lines
print('bacon')
and print('ham')
.
print('eggs')
if spam > 5:
print('bacon')
else:
print('ham')
print('spam')
每次增加一行的缩进都会开始一个新的块,并在相应的取消缩进之前结束。
spam = 0
if spam == 10:
print('eggs') # indent increased, block A
if spam > 5: # still block A
print('bacon') # still block A, indent increased, block B inside block A
else: # still block A, indent decreased, block B ended in line above
print('ham') # still block A, indent increased, block C inside block A
print('spam') # still block A, indent decreased, block C ended in line above
print('spam') # indent decreased, block A ended in line above
我是编程新手,正在尝试向 automatetheboringstuff.com 学习 Python。 在第 2 章的末尾,出现了以下问题。 甚至在我已经通过官方答案之后,我仍然一无所知。 请帮忙!
Q:8. Identify the three blocks in this code:
spam = 0 if spam == 10: print('eggs') if spam > 5: print('bacon') else: print('ham') print('spam') print('spam')
官方回答:
The three blocks are everything inside the if statement and the lines
print('bacon')
andprint('ham')
.print('eggs') if spam > 5: print('bacon') else: print('ham') print('spam')
每次增加一行的缩进都会开始一个新的块,并在相应的取消缩进之前结束。
spam = 0
if spam == 10:
print('eggs') # indent increased, block A
if spam > 5: # still block A
print('bacon') # still block A, indent increased, block B inside block A
else: # still block A, indent decreased, block B ended in line above
print('ham') # still block A, indent increased, block C inside block A
print('spam') # still block A, indent decreased, block C ended in line above
print('spam') # indent decreased, block A ended in line above