意外的缩进
我遇到了一个错误,提示是 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)
2 个回答
0
确保注释("""对单词进行排序""")不要直接写在def sort_words(words)下面。这样会导致错误。这样解决你的问题了吗?
3
你在代码里混用了制表符和空格,这样是不对的;可以用下面的命令运行你的脚本:
python -tt ex25.py
这样可以找出错误出在哪里。
然后把你的编辑器设置成只用空格来缩进代码,再重新调整一下你的文件缩进。你可以参考这个链接,了解如何把Notepad++设置成使用空格而不是制表符:如何配置Notepad++使用空格而不是制表符?