1

this is my first question on Stackflow. I want to create a pascal triangle with height of 'n'. Here is my code:

nb = input('input lines: ')
nb = int(nb)
array = [([1]+[0]*(nb-1))]*nb
for i in range(1, nb):
    for j in range(1 ,nb):
        array[i][j] = array[i-1][j-1] + array[i-1][j]

I want to get:

[1, 0, 0, 0, 0]
[1, 1, 0, 0, 0]
[1, 2, 1, 0, 0]
[1, 3, 3, 1, 0]
[1, 4, 6, 4, 1]

But the result is like this:

input lines: 5
[1, 4, 10, 20, 35]
[1, 4, 10, 20, 35]
[1, 4, 10, 20, 35]
[1, 4, 10, 20, 35]
[1, 4, 10, 20, 35]

I don't understand why every single line changed to the same. Especially I started the recursion from the second element in the second line.

Could someone explain the issue with my code. Thanks a lot

Jason Hu
  • 11
  • 1

0 Answers0