Python缩进之谜

Vin*_*ana 1 python loops enter

为什么我收到以下错误?最后一个print语句不应该是while循环的一部分.

>>> while n>= 0:
...     n = n-1
...     print(n)
... print ("TO A!!")
  File "<stdin>", line 4
    print ("TO A!!")
        ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

Bha*_*Rao 13

您需要enterwhile循环后按下以退出循环

>>> n = 3
>>> while n>=0:
...     n = n-1
...     print (n)
...                         # Press enter here
2
1
0
-1
>>> print ("To A!!")
To A!!
Run Code Online (Sandbox Code Playgroud)

注意: - ...暗示您仍然在while阻止

  • @ViniciusSantana把它放在一个方法中并运行它,我不认为python shell支持那个.. (2认同)