gpt4 book ai didi

java - Jython UnboundLocalError - 尝试了全局类型但仍然不起作用

转载 作者:行者123 更新时间:2023-12-01 04:31:44 25 4
gpt4 key购买 nike

我正在开发星球大战星系模拟器,我们使用的脚本语言是 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”...

最佳答案

将声明的变量放在等式的左侧。

def run(core, actor, target, commandString):

target = globalTarget
actor = globalActor

通过写:

globalTarget = target

您正在为 globalTarget 分配 target 的值。而不是创建一个名为 target 的变量。

关于java - Jython UnboundLocalError - 尝试了全局类型但仍然不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17912699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com