mardi 20 septembre 2016

Recursive if-statement python

Hi I am new to python and trying to implement a recursive function which fills up a table, but when running by program I get the following exception

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

def cost(i, j):
if table[i][j] == None:
    v1 = v2 = v3 = v4 = None
    if i > 0 and j > 0:
        print "case1"
        v1 = cost(i-1, j-1) + getSubCostMatrixValue(options[string1[i]], options[string2[j]])
    if i > 0 and j >= 0:
        print "case2"
        v2 = cost(i-1, j) + gapCost
    if i >= 0 and j > 0:
        print "case3"
        v3 = cost(i, j-1) + gapCost
    if i == 0 and j == 0:
        print "case4"
        v4 = 0
    print "Max:"
    print max(v1,v2,v3,v4)
    table[i][j] = max(v1,v2,v3,v4)
    return table[i][j]

the problem ocours i case 2 and case 3, as if the recursive call fails somehow, but i cannot find out why. I feel like it is something obvious

table is filled with None from the begining, gabCost is a int, getSubCostMatrixValue also returns an int.

Aucun commentaire:

Enregistrer un commentaire