-1

My code returns an exception from a documentation string, and I can't see the issue:

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()

Error:

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]

If the documentation string is commented out in the printWords function documentation string, the code works. I expect it is something simple yet I don't see it.

Thanks!

0

1 Answer 1

4

You've mixed tabs and spaces, causing Python to get confused about what statements are at what indentation level. On Python 3, you're supposed to get an error specifically notifying you of inconsistent mixing of tabs and spaces, but it looks like that's not happening for some reason.

Stop mixing tabs and spaces. Turn on "show whitespace" in your editor to make the problem visible, and see if there's a "convert tabs to spaces" tool to find and fix tabs for you.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.