为什么我不能在我的Python shell中执行这样的代码?

时间:2011-07-25 17:29:12

标签: python

为什么我无法在shell中运行以下行?

>>> try:
...     x = int("a lot")
... except items["error"] as e:
...     # Same as except ValueError as e
...     print("Couldn't convert")

错误讯息:

>>> try: ... x=int("a lot") File "<stdin>", line 2 x=int("a lot")<&>
         ^ 
IndentationError: expected an indented block 

2 个答案:

答案 0 :(得分:3)

适合我:

>>> items = {}
>>> items["error"] = ValueError
>>> try:
...     x = int("a lot")
... except items["error"] as e:
...     print "Couldn't convert"
...
Couldn't convert

答案 1 :(得分:0)

你的缩进是错误的。应该是:

>>> try:
...     x = int("a lot")
... except items["error"] as e:
...     print("Couldn't convert")