Why can't I execute such code in my Python shell? [closed]
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")
精彩评论