How can you update a Python function parameter each time it's called in a loop?

Viewed 135

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?

2 Answers

First off, list is the keyword for Pythons native list object. You should never use that as a variable name, as it would make it impossible to call the list constructor later on in your code, make type comparisons, and generally cause unexpected behaviour

Aside of that, the thing that you are missing is re-assigning the output of pascals_triangle to the input of the function in the next iteration of the loop. With that small change, it should work

I also moved the print statement outside of your function, such that you have more control of what is printed, and when.

import math


def pascals_triangle(lst):
    nextrow = []
    # numrows and numcols will be equivalent for all cases,
    # and increase for each call of the function.
    numrows = len(lst)
    numcols = len(lst)

    # count is the column we are currently looking at and will go from 1 to numcols.
    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

    return nextrow


cur_row = [1]
for _ in range(5):
    cur_row = pascals_triangle(cur_row)
    print(cur_row)
import math

def pascals_triangle(list):
    
    nextrow = []
    # numrows and numcols will be equivalent for all cases, 
    # and increase for each call of the function.
    numrows = len(list)
    numcols = len(list)
   
    # count is the column we are currently looking at and will go from 1 to numcols.
    count = 0
 
    for x in range(numcols+1):
        val = ((math.factorial(numrows)) / ((math.factorial(count)) * math.factorial(numrows - count)))
        nextrow.append(int(val))
        count += 1
    
    numrows += 1
    numcols += 1
    print(nextrow)
    
    return nextrow

list = [1]

print(list)

for x in range(5):
    list = pascals_triangle(list)

You almost had it. This should work, setting the "list" variable to the returned output from the function.

Note: "list" is a default function that shouldn't really be overwritten. I suggest using another variable name, as not to cause any confusing errors later on.

Related