我得到了
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 -tt ex25.py
检测错误所在。
然后将编辑器配置为仅使用空格进行缩进,并重新缩进文件。请参阅如何将 Notepad++ 配置为使用空格而不是制表符?
确保注释(“”“对单词进行排序”“”)不是直接位于 def sort_words(words) 下方。这会引发错误。这解决了您的问题吗?