2013-02-09 95 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