2013-02-09 54 views
2

我越来越IndentationError: unexpected indent即使我写的代码完全按照书(http://learnpythonthehardway.org/book/ex25.html意外缩进

我敢肯定,我正好用4个空格缩进哪来高清之后的”线所示”。

在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) 
+0

我正在使用Notepadd ++来编译我的代码,如果有问题 – 2013-02-09 11:18:32

回答

3

您正在混合标签和空格反正;运行脚本:

python -tt ex25.py 

检测错误的位置。

然后将您的编辑器配置为只有使用空格进行缩进并重新缩进文件。请参阅How does one configure Notepad++ to use spaces instead of tabs?

+0

谢谢Martijn。我配置了Notepad ++来替换Tab by Space。我仍然得到相同的错误,但我想我需要重新启动Notepad ++并手动重写整个代码? – 2013-02-09 11:39:26

+0

你可以用Notepad ++修复你的缩进:[Notepad ++ tabs to spaces](http://stackoverflow.com/q/455037)。你在脚本上运行了'python -tt'吗? – 2013-02-09 11:41:17

+0

是的,我运行python -tt ex25.py并获得以下内容: PS C:\ Users \ Kiedis \ python> python -tt ex25.py 文件“ex25.py”,第7行 word = words.pop (0) ^ TabError:使用缩进中的制表符和空格不一致 – 2013-02-09 11:46:37

0

确保评论(“”“对单词”“”进行排序)并不直接在def sort_words(单词)的下面。这产生了一个错误。这是否解决了你的问题?

+0

不,它不直接在def下面...第一个双引号是左起4个空格。并且“返回”是从左边4个空格 – 2013-02-09 11:17:38

+0

您是否确定在调用函数时提供了参数? Like sort_words(“Words”) 你已经说过你使用空格而不是制表符,所以不能成为问题。 – Traple 2013-02-09 11:24:29