0

I have a problem with my code. Because I can't access the list from outer function inside the nested one. I tried nonlocal but it gives me an error "non binding for nonlocal "a" found.

def func(x, y):
        nonlocal a
        nonlocal b
        if x >= 0 and y >= 0:
            if x == 0 and y == 0:
                return (func(x - 1, y - 1) + a[x] * b[y])

            else:
                return max(func(x - 1, y - 1) + a[x] * b[y], func(x - 2, y) + a[x - 1] * a[x], func(x, y-2) + b[y - 1] * b[y])
        elif x >= 1 and y < 0:
            return func(x - 2, y) + a[x - 1] * a[x]

        elif y >= 1 and x < 0:
            return func(x, y-2) + b[y - 1] * b[y]
        else:
            return 0

def tvshows(a, b):

    x = func(len(a) - 1, len(b) -1)
    return x

test_a = [23,45, 12, 16]
test_b = [33,13, 17, 18]

tvshows(test_a, test_b)

Does anyone know what the issue might be? Best Regards

GKroch
  • 73
  • 1
  • 9

1 Answers1

0

nonlocal requires the variable to have been defined before

I don't see any nested functions in your code. But you can use nonlocal inside the nested function (not the outer function) to reference a previously defined list.

Alec
  • 8,529
  • 8
  • 37
  • 63