我正在编写一个简单的类,使用递归生成Pascal三角形的第一行n
:
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()
我收到以下错误:
第45行 row.append(1)
....................... ^
IndentationError:unindent与任何外部缩进级别都不匹配
由于我在压痕中看不到任何问题,因此非常令人沮丧。我没有到这里来的是什么?