0

Hi I am trying to do a recursive function for Pascal triangle to calculate a specific value in a given row , below is my code and I keep getting the following error :

TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

My code so far:

def recursive_pascal(i,j):      
   if i == 0:          
     return 1
   elif j == 1:          
     return 1
   else:
     recursive_pascal(i-1,j-1) + recursive_pascal(i-1,j)

print(recursive_pascal(3,2))
Erik Godard
  • 5,930
  • 6
  • 30
  • 33

1 Answers1

5

You are not returning anything from the else clause. In Python, if you do not return anything from a function, a value of None will be returned. Your function will then attempt to add a None and an int (which you return from your base case), which is not possible and is why you are receiving that error. You should change it to

return recursive_pascal(i-1,j-1) + recursive_pascal(i-1,j)
Erik Godard
  • 5,930
  • 6
  • 30
  • 33