2

IndentationError: unexpected indent即使我编写的代码完全按照书中所示(http://learnpythonthehardway.org/book/ex25.html

我确定我使用正好 4 个空格来缩进“def”之后的行。

在 Windows 7 上使用 Python 2.7

这是我在 powershell 中遇到的错误:

PS C:\Users\Kiedis\python> python ex25.py
  File "ex25.py", line 3
    return sorted(words)
    ^
IndentationError: unexpected indent

这是我的代码的前 3 行:

def sort_words(words):
    """Sorts the words"""
    return sorted(words)
4

2 回答 2

3

无论如何,您都在混合制表符和空格;运行您的脚本:

python -tt ex25.py

检测错误所在。

然后将您的编辑器配置为使用空格进行缩进,并重新缩进您的文件。请参阅如何将 Notepad++ 配置为使用空格而不是制表符?

于 2013-02-09T11:29:06.210 回答
1

确保注释(“”“对单词进行排序”“”)不直接低于 def sort_words(单词)。这会引发错误。这解决了你的问题吗?

于 2013-02-09T11:12:22.223 回答