开发者

python: why "IndentationError: expected an indented block" for this code?

Refer to Listing 9. Iteration and a dictionary

>>> d = {0: 'zero', 3: 开发者_JAVA技巧'a tuple', 'two': [0, 1, 2], 'one': 1}
>>> for k in d.iterkeys():
... print(d[k])
  File "<stdin>", line 2
    print(d[k])
        ^
IndentationError: expected an indented block

Why?


Indentation level of your statements is significant in Python.


Python 3 doesn't have iterkeys. Just use:

for k in d:
    print(d[k])

or even better:

for v in d.values():
    print(v)


Even when using the Python interactive interpreter, you need to make sure you have some indentation for a new block of code.

This:

>>> for k in d.iterkeys():
... print(d[k])

Should be this:

>>> for k in d.iterkeys():
...     print(d[k])

As an aside: that link has a number of errors in what should be the expected output, possibly some copy/paste problem?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜