为什么我收到以下错误?最后一个print
语句不应该是while
循环的一部分。
>>> while n>= 0:
... n = n-1
... print(n)
... print ("TO A!!")
File "<stdin>", line 4
print ("TO A!!")
^
SyntaxError: invalid syntax
答案 0 :(得分:13)
您需要在while
循环后按 enter 退出循环
>>> n = 3
>>> while n>=0:
... n = n-1
... print (n)
... # Press enter here
2
1
0
-1
>>> print ("To A!!")
To A!!
注意: - ...
表示您仍然在while
区块
答案 1 :(得分:4)
默认的python shell可以正常输入,但它确实无法理解从剪贴板粘贴。真正的解决方案是安装ipython
,这是python的高级shell,有许多细节:
% ipython3
Python 3.4.2 (default, Oct 8 2014, 13:08:17)
Type "copyright", "credits" or "license" for more information.
IPython 2.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: n = 5
In [2]: while n >= 0:
...: n = n-1
...: print(n)
...: print ("TO A!!")
...:
4
3
2
1
0
-1
TO A!!
In [3]:
答案 2 :(得分:3)
我猜错误出现了,因为python shell不支持。它希望你一次做一件事。我在python 2.7 shell中做了同样的事情,它说:
File "<pyshell#4>", line 4
print 'to all'
^
IndentationError: unindent does not match any outer indentation level
当我在我的python 3.4 shell中做同样的事情时,它说:unexpected indent.