意外索引

2024-05-19 19:18:00 发布

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

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

我确信我正使用4个空格来缩进“def”后面的行。

在Windows7上使用Python2.7

以下是我在powershell中遇到的错误:

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

下面是我代码的前三行:

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

Tags: 代码pyorghttpreturndefhtmlwords
2条回答

不管怎样,您正在混合制表符和空格;运行脚本时使用:

python -tt ex25.py

以检测错误所在的位置。

然后将编辑器配置为仅使用空格缩进,并重新缩进文件。见How does one configure Notepad++ to use spaces instead of tabs?

请确保注释(“”对单词排序“”)不直接位于def sort庠words(单词)的下面。这会引发错误。这解决了你的问题吗?

相关问题 更多 >