UnboundLocalError when variable is clearly local

Question:

I have a program similar to this:

curpuz = 1

def puzzle(req,_):
    puzzledat = json.loads(open('puzzles.txt','r').read())
    puzzleque = puzzledat[str(curpuz)]['q']
    req.say('Current puzzle: ' + puzzleque + ' - !ans <answer>')

def puzzlea(req,arg):
    puzzledat = json.loads(open('puzzles.txt','r').read())
    puzzleans = puzzledat[str(curpuz)]['a']
    if arg[0] == puzzleans:
        req.say('%tip ' + req.nick + ' 50 -- ' + req.nick + ' got the correct answer! Use !puzzle to get the next puzzle!')
        try:
            puzzledat = json.loads(open('puzzles.txt','r').read())
            curpuz += 1
            puzzleque = puzzledat[str(curpuz)]['q']
            puzzleans = puzzledat[str(curpuz)]['a']
        except:
            req.say("Uh oh no more puzzles :(")
            puzzleans = 'no more'
    else:
        req.reply('Incorrect')

But i continue to get an UnboundLocalError("local variable 'curpuz' referenced before assignment",) even though the variable is clearly local.

Asked By: TheDoctor

||

Answers:

try this

def puzzlea(req,arg):
    global curpuz
Answered By: Rafael Barros
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.