0

I have a .py config file that sets up a set of global variables. Among them is a variable called nMin. kNeighbors is another global variable defined in the exact same way as nMin. All the other variables included in the block are local variables

for i in range(PopLength):
    noveltyMetric[i] = np.average(behavior[i][0:kNeighbors])
    if noveltyMetric[i] > nMin:
        ...

In the loop, the program references kNeighbors with no issue, but on the next line I get the error:

local variable 'nMin' referenced before assignment"

I'm running this in PTVS 2017 and when it stops & displays the bug (while paused on that line), I call nMin in the "Immediate Window" and it calls the value correctly with no error. When I call the statement noveltyMetric[i] > nMin it returns a Boolean with no error. But for some reason it still fails to execute the line in the code and insists I'm referencing it before it's defined.

KGulin
  • 1
  • If this code is inside a function, and you're assigning to `nMin` anywhere in that function, then `nMin` is a local variable. The fact that there's also a global variable with the same name doesn't matter. If you don't want to create a local variable that hides the global, add a `global nMin` statement to the top of the function. – abarnert Jul 26 '18 at 19:09
  • That solved it. Thank you! – KGulin Jul 26 '18 at 19:18

0 Answers0