Home Python: global not defined / local var referenced before assignment
Reply: 0

Python: global not defined / local var referenced before assignment

user1357
1#
user1357 Published in April 27, 2018, 6:42 am

In this code (part of a function definition)

    global aCVals
    global aCKeys
    i = 0
    allCandidates = self.getCandidateDictionary(musicalSentence)
    constrainedCandidates = {}
    aCVals = list(allCandidates.values())
    aCKeys = list(allCandidates.keys())

    for i in range(len(acVals)):
        if acVals[i] == possiblePitches[i]:
            constrainedCandidates.append(possiblePitches[i])
    for i in range(len(aCVals)):
        if aCKeys[i] in possiblePitches:
            constrainedCandidates.append(aCKeys[i])
        elif aCKeys[i] == '$:::$': 
            constrainedCandidates.append(aCKeys[i])
    isDictionaryEmpty = bool(constrainedCandidates)
    if isDictionaryEmpty == True:
        q = self.weightedChoice(constrainedCandidates)
        return q
    else: 
        tup = (random.choice(possiblePitches).append(4), random.choice(NOTE_DURATIONS))
        return tup

I get the error:

 NameError: global name 'acVals' is not defined

The reason I added the globals is because before I put them there I would get this error:

local variable 'acVals' referenced before assignment

Please help, and please be thorough in explanation so I gain a better understanding of the concept I'm missing here

Thanks

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.322263 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO