开发者

Why can't I execute such code in my Python shell? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

Why am I not able to run the following lines in my shell?

>>> try:
...     x = int("a lot")
... except items["error"] as e:
...     # Same as except ValueError as e
...     prin开发者_如何学Got("Couldn't convert")

Error message:

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


Works for me:

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


Your indentation is wrong. Should be:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜