在分配之前引用的'pot'

时间:2014-01-04 05:10:20

标签: python

当我尝试在我的Point.py中使用我的def getLotteryGame()时,它一直告诉我

'pot' refernced before assignment. 

这是代码:

pot = 1

class Point:

    def buyLottery(name, amount):
       Point.startPoint(name)
       amount = int(amount)
       multiplier = random.randint(amount, 217)
       pot = int(pot+multiplier)
       if name not in players:
          if Point.getCost(name, amount) == True:
             players.append(name)
             return "%s has joined the the lottery for %s P$ which magically grew to %s P$!" % (name.title(), Point.amountCost(name, amount, multiplier)

1 个答案:

答案 0 :(得分:2)

在这一行

pot = int(pot+multiplier)

即使在分配任何值之前,您也在使用pot。如果你想使用global pot,那么你需要明确告诉Python,就像这样

def buyLottery(name, amount):
    global pot
    ...
    pot = int(pot+multiplier)