I've written a function in Jupyter that takes in a list containing the values for one row of Pascal's Triangle and then produces the following row. Due to the requirements of the project, I need to start with a list that contains only the integer of 1, and then create a loop that calls the function 5 times (leaving me with 6 total rows).
import math
def pascals_triangle(list):
nextrow = []
numrows = len(list)
numcols = len(list)
count = 0
for x in range(numcols+1):
val = ((math.factorial(numrows)) / ((math.factorial(count)) * math.factorial(numrows - count)))
nextrow.append(val)
count += 1
numrows += 1
numcols += 1
print(nextrow)
return nextrow
list = [1]
print(list)
for x in range(5):
pascals_triangle(list)
Here's my problem: I can't get my function parameter or count to update in a way that prints out all 6 incrementing rows. Right now, it just prints the same return value five times.
Expected output:
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
Real output:
[1]
[1, 1]
[1, 1]
[1, 1]
[1, 1]
[1, 1]
How can I make the function use the previous return value each time it is called, or get my numrows/numcols to increment for the next function call?