class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
index = len(candidates) - 1
ans = []
def recursiveAlgo(currIndex):
if currVal == target:
ans.append(currValList.copy())
return
if currVal > target:
currValList.pop()
currVal =- candidates[currIndex]
return
currVal += candidates[currIndex]
currValList.append(candidates[currIndex])
attemptedIndex = currIndex
while attemptedIndex == currIndex:
recursionAlgo(attemptedIndex)
attemptedIndex -= 1
currValList.pop()
currVal -= nums[currIndex]
while index >= 0:
currVal = 0
currValList = []
recursiveAlgo(index)
index -= 1
return ans
This code is giving me an unboundlocal error at currVal == target: but i don't really understand. When the function gets called, currVal and currValList would've been executed already and so it should have access to it since it's called right after. Could someone explain how scoping works here?