0

I am new to Python. I get an error that says "undefined Variable". I could not figure it out. The statement in the code is the following:

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)
2
  • I think, you are calling the function 'Loop' before defining it. Commented Jun 26, 2012 at 23:09
  • Perhaps it means that the variable Python tells you is undefined (which you have neglected to tell us), might be undefined? Python will also tell you the line at which it's trying to use the undefined variable; maybe defining it earlier than that line would help. Could be worth adding even a dummy definition above that line such as Loop = 0 and seeing whether you at least get a different error. Commented Jun 27, 2012 at 0:45

2 Answers 2

3

You're calling your Loop() function before you have defined it.

Sign up to request clarification or add additional context in comments.

3 Comments

In addition, where's the Classroom class?
Further to this, you seem to be infinitely and exponentially recurring over the Loop function (assuming all lines of code after your def Loop line are supposed to be indented by one).
Looks like the indentation's been fixed and you are indeed invoking infinite recursion.
0

First you have to organize:

class classroom,
    def loop

Inside your function loop write in globals:

global m_count
global h_count
global t_count

and finally change the if statements:

if HardConstraintClassroomMet == False and HardConstraintMutatedMet == False:
        h_count = h_count + 1
        t_count = t_count + 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.