2016-08-14 46 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对什么语句处于缩进级别感到困惑。在Python 3中,你应该得到一个特别通知你标签和空格混合不一致的错误,但是看起来这不是由于某种原因而发生的。

停止混合标签和空格。打开编辑器中的“显示空白”以使问题可见,并查看是否存在“将制表符转换为空格”工具来查找和修复制表符。