意外缩进[重复]

问题描述 投票:0回答:2

我得到了

IndentationError: unexpected indent
,即使我完全按照书中所示编写了代码(艰难地学习Python

我确信我使用了四个空格来缩进“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

这是我的代码的前三行:

def sort_words(words):
    """Sorts the words"""
    return sorted(words)
python indentation
2个回答
3
投票

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

python -tt ex25.py

检测错误所在。

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.