2016-08-14 6 views
0

私のコードは、説明文字列から例外を返し、私は問題を見ることができない。シンプルなPythonドキュメントの例外が

def printBreak(title): 
    """Prints a dotted line""" 
    print("------" + title + "------") 

def printWords(theWords): 
    """Prints the passed array with word length and word""" 
    for w in theWords: 
     print(len(w), w) 
    print("") 

def sortWordsByLength(theWords): 
    """Word array sorted by length""" 
    for w in theWords: 
     for i in range(len(wordsSortedByLength)): 
      if len(w) > len(wordsSortedByLength[i]): 
       wordsSortedByLength.insert(i,w) 
       break 
     else: 
      wordsSortedByLength.append(w) 


def main(): 
    printBreak("Initial word array") 
    printWords(words) 
    printBreak(sortWordsByLength.__doc__) 
    sortWordsByLength(words[1:]) 
    printWords(wordsSortedByLength) 

# Sort some strings 
words = ['happy', 'cat', 'window', 'appetite', 'gophery', 'it', 'perky'] 
wordsSortedByLength = [words[0]] 
main() 

エラー:

File "/Users/bevilacm/source/Python/Scripts/sortWords.py", line 7 
for w in theWords: 
       ^
IndentationError: unindent does not match any outer indentation level 
[Finished in 0.1s with exit code 1] 

ドキュメンテーション文字列がでコメントアウトされている場合printWords関数のドキュメント文字列、コードは動作します。私はそれが何かシンプルだと思うが、私はそれを見ない。

ありがとうございます!

答えて

4

タブとスペースが混在しているため、インデントレベルの文が混乱することがあります。 Python 3では、タブとスペースの矛盾した混合について特に通知するエラーが発生するはずですが、何らかの理由でそれが起こっていないようです。

タブとスペースの混合を停止します。エディタで「空白を表示」をオンにして問題を表示し、「タブをスペースに変換」ツールがタブを見つけて修正するかどうかを確認します。

関連する問題