当变量显然是本地的时,UnboundLocalError

时间:2014-06-17 15:24:29

标签: python global

我有一个类似的程序:

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')

但即使变量显然属于本地,我仍然会得到UnboundLocalError("local variable 'curpuz' referenced before assignment",)

1 个答案:

答案 0 :(得分:1)

试试这个

def puzzlea(req,arg):
    global curpuz