我正在为星球大战星系的模拟器工作,我们正在使用的脚本语言是Jython。我无法弄清楚如何在def之外定义变量。这是错误..
UnboundLocalError:在赋值之前引用的局部变量'actorFunds'
代码(切出很多不必要的......没有上课)
commandArgs = ""
commandLength = 0
globalTarget = SWGObject
globalActor = CreatureObject
tipAmount = 0
actorFunds = 0
totalLost = 0
bankSurcharge = 0
def run(core, actor, target, commandString):
global globalTarget
global globalActor
global commandArgs
global commandLength
global tipAmount
global bankSurcharge
global actorFunds
global totalLost
globalTarget = target
globalActor = actor
commandArgs = commandString.split(" ")
commandLength = len(commandArgs)
tipAmount = commandArgs[0]
bankSurcharge = int(0.05) * int(tipAmount)
actorFunds = actor.getBankCredits()
totalLost = int(tipAmount) + bankSurcharge
.......
return
def handleBankTip(core, owner, eventType, returnList):
if eventType == 0:
if int(totalLost) > globalActor.getBankCredits():
globalActor.sendSystemMessage('You do not have ' + str(totalLost) + ' credits (surcharge included) to tip the desired amount to ' + globalTarget.getCustomName() + '.')
return
if int(tipAmount) > 0 and int(actorFunds) >= int(totalLost):
return
return
当我找到actorFunds时,它会提供错误...
我搜索了很长时间,无法弄清楚原因。我希望它像java一样简单,带有“this”......
答案 0 :(得分:0)
将声明的变量放在等式的左侧。
def run(core, actor, target, commandString):
target = globalTarget
actor = globalActor
写作:
globalTarget = target
您正在为globalTarget分配target的值。而不是创建一个名为target的变量。