代码:
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for i in range(3):
... print(1)
... print(2)
File "<stdin>", line 3
print(2)
^
SyntaxError: invalid syntax
预期产出:
1
1
1
2
通过多次测试,我知道当缩进块较低时,python交互式shell返回无效语法错误。
测试代码:
if true:
print("test")
print(test)
输出:
File "<stdin>", line 3
print("test")
^
SyntaxError: invalid syntax
要解决此问题,我必须使用回车键。
>>> for i in range(3):
... print(1)
...
1
1
1
>>> print(2)
2
但为什么呢?为什么我必须使用enter分隔两个代码?我想知道原因。
注意:我的英语不好,很难描述。 &#34;缩进结束&#34;和&#34;缩进块有较低的&#34;意味着那种情况
if true: //no indentation, no tab or space
print(1) //in if syntax, we have to make indentation
print(2) //when if end, We don't make indentation. Compare to line 2, 'indentation block has lower' and 'indentation had ended'
我不认为这是正确的表达,所以你可能有点困惑。如果您理解这一点,请编辑此问题并使其正确。