0

我是 Python 新手。我收到一条错误消息,显示“未定义的变量”。我想不通。代码中的语句如下:

Classroom1 = Classroom(FirstCourseStartTime = 8, LastCourseEndTime = 7.75)
m_count = 0 
h_count = 0
t_count = 0
l_count = 0
loopsize = 10000
Loop(Classroom1, m_count, h_count, t_count, l_count, loopsize)
def Loop(Classroom1, m_count, h_count, t_count, l_count, loopsize):
    for i in range(0, loopsize):
        Mutated = Classroom()
        Mutated = Classroom1.CopyOfClassroom(Mutated)
        Mutated.Mutate()
        HardConstraintClassroomMet = Classroom1.ComputeHardConstraint()
        HardConstraintMutatedMet = Mutated.ComputeHardConstraint()
        if HardConstraintClassroomMet == False and HardConstraintMutatedMet == False:
            h_count + 1
            t_count + 1
            Classroom1.Mutate()
            continue
        if Mutated.FitnessValue() > Classroom1.FitnessValue():
            m_count + 1
            t_count + 1
            Classroom1 = Mutated
        if Classroom1.ComputeHardConstraint() == False:
            l_count + 1
            temp = l_count*loopsize
            print "Unable to meet hard consraints in %d" % temp
        Loop(Classroom1, m_count, h_count, t_count, l_count, loopsize)
4

2 回答 2

3

您在Loop()定义函数之前调用了它。

于 2012-06-26T23:08:20.823 回答
0

首先你必须组织:

class classroom,
    def loop

在你的函数循环中写入全局变量:

global m_count
global h_count
global t_count

最后更改 if 语句:

if HardConstraintClassroomMet == False and HardConstraintMutatedMet == False:
        h_count = h_count + 1
        t_count = t_count + 1
于 2012-07-11T16:10:20.383 回答