0

I'm writing a simple class that generates the first n lines of the Pascal Triangle using recursion:

class PascalTriangle:
    """Returns the first n lines of the Pascal Triangle. Recursive."""

    def __init__ (self, n):
        assert(n > 0)
        self.lst[0][0] = 1
        self.lines = n
        calculate (1)

    def __call__(self, n):
        assert(n > 0)
        self.lst[0][0] = 1
        self.lines = n
        calculate (1)

    def calculate (self, k):
        if k ==  self.lines:
            return self.lst
        else:   
            row = [ ]
            row.append(1)     # <------- line 45
            for  i in range(1, k-1):
                row.append(self.lst[k-1][i-1] + self.lst[k-1][i])
            row.append(1) 
            self.lst.append(row)
            calculate(k+1)

    def printTriangle(self):
        for row in self.lst:
            print(row)


ps = PascalTriangle(2)
ps.printTriangle()

ps3 = PascalTriangle()
ps3(3)          
ps3.printTriangle()

I'm getting the following error:

line 45
row.append(1)
.......................^
IndentationError: unindent does not match any outer indentation level

It has been very frustrating as I can't really see any problems in the indentation. What am I not getting here?

Ziezi
  • 6,375
  • 3
  • 39
  • 49
  • 2
    this error usually means that you provided erroneous indents somewhere in your code. may be 5 spaces, instead of 4 or wrong tabulation. i don't know if stackowerflow shows formatting correctly. – Chiefir May 02 '18 at 16:24
  • 3
    Sounds like mixed tabs and spaces. The tab/space mixture in the edit view is suspicious. – user2357112 May 02 '18 at 16:25
  • 1
    I see a mix of tabs and spaces in the code you posted here on SO; see [this screenshot from your text in Sublime Text, where dots are spaces and dashes are tabs](https://i.stack.imgur.com/eWSZd.png). Don't do that, configure your editor to only use spaces for indentation. – Martijn Pieters May 02 '18 at 16:35
  • 1
    toggling *"show invisibles"* (or equivalent) in your editor is crucial for whitespace-sensitive languages – Mulan May 02 '18 at 19:20

0 Answers0