Hack Forums
[Python] Iterating through a list of objects - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Iterating through a list of objects (/showthread.php?tid=6049067)



Iterating through a list of objects - Bora or Hyuna - Feb 13, 2020

Hi there,

I am trying to iterate through a list of objects, what I want to do is if only 1 object in the list returns true, the program should continue until all the other objects return True.

Here is my update method:
Code
def next_frame(self):
        global finish
        if(Clock.time%(random.randrange(1,60, 2))==0):
            self.set_frame(self.frame + 1)
        if(self.done()):
            finish = True


Here is my method for returning a boolean value:
Code
def done(self):
        global finish
        if((self.frame == (self.grid_s[0]*self.grid_s[1])-1)):
            #finish = True
            return True
        else:
            return False
At the current moment what this does is, when I run the program I initialise a few objects so they are run, but after my first object in the list returns True, all my other objects stops what they are doing and my program stops working. How can I make changes so that the program will stop only after all the objects have returned True. Please let me know if you need to see more of my code, thanks!


RE: Iterating through a list of objects - lockhead - Feb 13, 2020

Without more code I can't say for sure but I think what you're doing is setting a global variable "finish" to the result of this condition
PHP Code

<?php 
self
.frame == (self.grid_s[0]*self.grid_s[1])-1) 

So if this is true then finish will also be set to true and i presume finish is the control variable that stops the main loop for something.
So I guess change the condition so that it fits your goal, I dont really know how tho since I don't really understand without any context.


RE: Iterating through a list of objects - Bora or Hyuna - Feb 14, 2020

(Feb 13, 2020 11:09 AM)lockhead Wrote:  Without more code I can't say for sure but I think what you're doing is setting a global variable "finish" to the result of this condition
PHP Code

<?php 
self
.frame == (self.grid_s[0]*self.grid_s[1])-1) 

So if this is true then finish will also be set to true and i presume finish is the control variable that stops the main loop for something.
So I guess change the condition so that it fits your goal, I dont really know how tho since I don't really understand without any context.

A friend of mine was able to help me yesterday and it finally works all I needed was to remove the explosions from the list after it was done. My god the solution was so simple.