python:为什么这段代码需要“IndentationError:expected an indented block”?

2024-05-29 01:42:52 发布

您现在位置:Python中文网/ 问答频道 /正文

参考Listing 9. Iteration and a dictionary

>>> d = {0: 'zero', 3: '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

为什么?


Tags: andinfordictionarystdinlineonefile
3条回答

Python 3没有iterkeys。只需使用:

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

或者更好:

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

即使在使用Python交互式解释器时,也需要确保对新的代码块进行了一些缩进。

这:

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

应该是这样的:

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

顺便说一下:该链接在预期输出中有很多错误,可能是一些复制/粘贴问题?

相关问题 更多 >

    热门问题