0

I'm trying to do a recursive function that generates a pascal's triangle up till the nth row, n being the user input. This is my code so far:

def printPascal(l,n):
    while n != 1:
        temp = [None]*(len(l)+1)
        temp[0] = 1
        temp[len(l)] = 1
        for i in range(1,len(temp)-1):
            temp[i] = l[i] + l[i-1]
            l = temp
            print(temp)
        n = n-1
        printPascal(l,n)


n = int(input("Enter a value for n:"))
l = [1,1]
printPascal(l,n)

And this is the error it gives me:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\test.py", line 16, in <module>
    printPascal(l,n)
  File "C:\Users\User\Desktop\test.py", line 11, in printPascal
    printPascal(l,n)
  File "C:\Users\User\Desktop\test.py", line 7, in printPascal
    temp[i] = l[i] + l[i-1]
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

The thing is i kinda understand the issue and have tried tracing it to no avail. I know that somehow in the temp[i] = l[i] + l[i-1] code either the l[i] or l[i -1] is a "None" and i don't know why.

Thanks for your time and help in this little predicament of mine.

4 Answers 4

1

You have too much of your code inside the "for" loop. You replace the value of l with the value of temp before you've finished filling in all the values in temp.

The for loop should only contain the first statement.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot, that fixed up my problem. I didn't realize that the loop included the other lines as well.
1

There was a little indentation error in your code.

def printPascal(l,n):
    while n != 1:
        temp = [0]*(len(l)+1)
        temp[0] = 1
        temp[len(l)] = 1
        for i in range(1,len(temp)-1):
            temp[i] = l[i] + l[i-1]
        l = temp
        print(temp)
        n = n-1
        printPascal(l,n)

2 Comments

It's the indentation error that's the culprit, if I'm reading properly. Initializing with 0 just hides the problem.
Yes you're right about that. But that was what enabled me to actually realize the error :)
1

Instead of fixing your code, I'll show you an optimized approach:

def pascal_triangle(n, triangle=[[1]]):
    if n > len(triangle):
        last_row = triangle[-1]
        next_row = [a+b for (a, b) in zip([0] + last_row, last_row + [0])]
        return pascal_triangle(n, triangle + [next_row])
    return triangle

How you can run it:

n = int(input("Pascal's triangle size: "))
print(*pascal_triangle(n), sep="\n")

Example output for input 9:

[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]

Or using a prettier printing command:

print(*[" ".join(map(str, line)).center(40) for line in pascal_triangle(9)], sep="\n")

It would look like this:

                    1                         
                   1 1                        
                  1 2 1                       
                 1 3 3 1                      
                1 4 6 4 1                     
              1 5 10 10 5 1                   
             1 6 15 20 15 6 1                 
           1 7 21 35 35 21 7 1                
          1 8 28 56 70 56 28 8 1              

See this code running on ideone.com

Comments

0

You get this error because you not calculate and you not return sufficient information to the function.

try to debug or to follow your code..

You will fined the problem easily :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.