赋值前引用的局部变量 (CounterOne)

local variable referenced before assignment (CounterOne)

我 运行 添加 CounterOne 变量之前的代码 运行 没问题。

但是在添加 CounterOne 变量后,编译器开始给我以下错误。

"local variable 'CounterOne' referenced before assignment"

CounterOne = 0.00

def AAPILoad():
    return 0

def AAPIInit():
    return 0

def AAPIManage(time, timeSta, timeTrans, acycle):
    AKIPrintString( "AAPIManage" )      

    xy = doSomething() #Read Number of Sections
    for i in range (xy):
        id = getID(i) #Read the identifier of a section
        if (id==331):
            xyz = DoCal(id,True) #Read the number of vehicles in a section
            for j in range (xyz):
                Calculaitons

                if (0<=distanceFromTrafficLight<=300):
                    if ( condition == False) :
                        do calculations
                    else :
                        print ("Condition failed")

                    if ( Condition):  #Cruising 
                        Calval = 0.233+2*someValue

                        CounterOne = CounterOne + Calval
    return 0
CounterOne

不是全局变量,这就是抛出错误的原因。

您可以使其成为全局的,这不是 recommended,或者将 CounterOne 值传递给函数。

对于全局方法:

def AAPIManage(time, timeSta, timeTrans, acycle):
    global CounterOne
   AKIPrintString( "AAPIManage" )

对于函数方法:

def AAPIManage(time, timeSta, timeTrans, acycle,CounterOne):

AND RETURN CounterOne 而不是零