def triangle(n):
if n == 0:
return []
if n == 1:
return [[1]]
else:
before = triangle(n - 1)
calculate = [0] + before[-1] + [0]
now = []
i = 0
while i < len(calculate) - 1:
now.append(calculate[i] + calculate[i + 1])
i = i + 1
print now
return before.append(now)
#Expected output:
print triangle(0)
#>>> []
print triangle(1)
#>>> [[1]]
print triangle(2)
#>> [[1], [1, 1]]
print triangle(3)
#>>> [[1], [1, 1], [1, 2, 1]]
print triangle(6)
#>>> [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]]
in this code, when the input value is 2, returned None. if the input velue is more then 2, the code referenced triangle(2), and Nonetype object error occurred
with a little googleing, I know the this error is because of None value.
>>> bar = "something"
>>> foo = None
>>> print foo + bar
TypeError: cannot concatenate 'str' and 'NoneType' objects
but i think the code not concatenate None velue.
return before.append(now)
this code concatenate before and now. so i wrote debug code like this.
print "before is"
print before
print "now is"
print now
return before.append(now)
the result is
triangle(2)
before is
[[1]]
now is
[1, 1]
None
before and now, both are not None!
I already re-write the code several times, but all code has error. Pleace help me! i using python 2.
it is first time to using stackoverflow, so pleace notice me if i contrary to regulations