Bizarre looping in python?
Question: I'm new to python, but I come from a basic java background. There are learning
curves to face, and so I'm having troubles. With this loop particularly...

    
    
    from random import randint;
    class simpleAI:
    inputMatter = 0;
    inputEnergyString = 0;
    inputEnergy = 0;
    resultMatter = 0;
    resultEnergy = 0;
    inputMatterMemory = [];
    inputEnergyMemory = [];
    resultMatterMemory = [];
    resultEnergyMemory = [];
    searchIndex = 0;
    found = 0;
    lookin = 0;
    lookingEnergy = 0;
    
    def cycle(self):
        self.inputMatter = input("Matter: ");
        self.inputMatter = int(self.inputMatter, 10);
        self.inputMatterMemory.append(self.inputMatter);
        self.inputEnergyString = input("Energy: ");
        self.inputEnergy = int(self.inputEnergyString, 10);
        self.inputEnergyMemory.append(self.inputEnergy);
        #Check for an event that resulted in positive energy
        while(self.searchIndex < self.resultEnergyMemory.__len__()):
            print("we are foring!");
            if(self.inputEnergyMemory.__getitem__((self.inputMatterMemory.index(self.inputMatter))) == self.inputEnergy):
                 print("we are iffy!");
                 self.resultMatter = self.resultMatterMemory.__getitem__(self.inputMatterMemory.index(self.inputMatter));
                 self.resultEnergy = self.resultEnergyMemory.__getitem__(self.inputEnergyMemory.index(self.inputEnergy));
                 self.found = 1;
                 break;
    
            else:
                self.searchIndex = self.searchIndex + 1;
    
        if(self.found == 0):
            self.resultMatter = randint(0, 256);
            self.resultMatterMemory.append(self.resultMatter);
            self.resultEnergy = randint(0, 256);
            self.resultEnergyMemory.append(self.resultEnergy);
            print("Creating new pair...");
            print("Result matter: ", self.resultMatter);
            print("Result Energy: ", self.resultEnergy);
        elif(self.found == 1):
            print("Found positive pair from memory");
            print("Result matter: ", self.resultMatter);
            print("Result memory: ", self.resultEnergy);
    
        self.inputMatter = 0;
        self.inputEnergyString = 0;
        self.inputEnergy = 0;
        self.resultMatter = 0;
        self.searchIndex = 0;
        self.found = 0;
    

It produces the right result the first iteration, but falls back to "creating"
a new pair on other iterations, even when it's supposed to return a pair when
entered in before. What am I doing wrong?


Answer: You made a lot of extra work for yourself. Instead of trying to maintain
multiple parallel arrays, you can simply make a separate class to hold all the
data together and just have a single array of that class to loop through.

    
    
    from random import randint;
    
    class MatterEnergyData:
        def __init__(self, inputMatter, inputEnergy, resultMatter, resultEnergy):
            self.inputMatter = inputMatter
            self.inputEnergy = inputEnergy
            self.resultMatter = resultMatter
            self.resultEnergy = resultEnergy
    
        def __str__(self):
            return "Result matter: %s\r\nResult energy: %s" % \ 
                self.resultMatter, self.resultEnergy
    
    class simpleAI:
        matterEnergyDataPoints = []
    
        def cycle(self):
            inputMatter = int(input("Matter: "))
            inputEnergy = int(input("Energy: "))
            resultObj = None
            for obj in self.matterEnergyDataPoints:
                if obj.inputEnergy == inputEnergy and obj.inputMatter == inputMatter:
                    print("Found positive pair from memory")
                    resultObj = obj
                    break
    
            if resultObj == None:
                print("Creating new pair...")
                resultObj = MatterEnergyData(inputMatter, inputEnergy, randint(0, 256), randint(0, 256))
                self.matterEnergyDataPoints.append(resultObj)
    
            print resultObj
    

