[Tutor] remove instances from a list

Ara Kooser ghashsnaga at gmail.com
Mon Sep 17 01:58:35 CEST 2007


Hello all,

   On my continuing quest to grapple with OO programming Kent showed
me that I could call instances and store them in a list like:
yeasts = [Yeast("Red_1","Red","ade","lys"),Yeast("Yellow_1","Yellow","lys","ade"),
          Yeast("Red_2","Red","ade","lys"),Yeast("Yellow_2","Yellow","lys","ade")]

Give certain conditions I want the yeast cell to die. Kent suggested I
use something this:
 yeasts = [yeast for yeast in yeasts if yeast.isAlive()] to clear out
dead yeast.

Is the translation for the above line of code into pseudocode?
yeast for every yeast in the list yeasts if the yeast method returned isAlive()

Or is this meant to be a true or false return? I have included the
code below. Thank you again.

Ara


###############
CODE BELOW
###############

#########################################################################
# Yeast Model
# By Ara Kooser
# Version beta
#
#Thanks to Kent, Michael, and Alan for suggestions and help
##########################################################################
import random
chemicals = []

class Yeast:

    def __init__(self,name,group,need,release):
#name can be any, group is either red or yellow, need/release lys or ade
        self.name = name
        self.group = group
        self.need = need
        self.release = release
        self.growth = 0
        self.life = 0
        self.starve = 0
        self.alive = True

#METHODS

    def setup(self):
#Sets up the random variable for each yeast before the game logic loop
        if self.group == "Red":
            a = random.randrange(40,160)
            self.life = a
            self.growth = 5
            aa = random.randrange(20,50)
            self.starve = aa
        elif self.group == "Yellow":
            b = random.randrange(20,80)
            self.life = b
            self.growth = 3
            bb = random.randrange(10,30)
        elif self.group == "Defect":
            pass
        else:
            pass

    def chem_need(self):
        if self.need == "ade":
            if self.need in chemicals:
                print self.name,"is taking ade"
                self.life = self.life+1
                chemicals.remove(self.need)
                print chemicals
                print "Life", self.life
                print
            else:
                print self.name, "found no ade present"
                self.life = self.life-1

        elif self.need == "lys":
            if self.need in chemicals:
                print self.name," is taking lys"
                self.life = self.life+1
                chemicals.remove(self.need)
                print chemicals
                print "Life",self.life
                print
            else:
                print self.name, "found no lys present"
                self.life = self.life-1

        else:
            pass

    def near_death(self):
#release of aa in the environment near cell death
        if self.life <= self.starve:
            c = random.randrange(1,3)
            if c == 1:
                print self.name, "-- Releasing", self.release
                chemicals.append(self.release)

            if c == 2:
                print self.name, "-- Releasing", self.release
                chemicals.append(self.release)
                chemicals.append(self.release)

            if c == 3:
                print self.name, "-- Releasing", self.release
                chemicals.append(self.release)
                chemicals.append(self.release)
                chemicals.append(self.release)

    def isAlive(self):
        if self.life > 0:
            self.alive = True
        else:
            self.alive = False

    def growth(self):
        pass

###########################################################################################
#Program Starts Here
###########################################################################################

#Stores the instances in a list for easier use. Later versions will
allow the user to add
#instances

#Instance are called: Yeast, name, group, aa need, aa release

yeasts = [Yeast("Red_1","Red","ade","lys"),Yeast("Yellow_1","Yellow","lys","ade"),
          Yeast("Red_2","Red","ade","lys"),Yeast("Yellow_2","Yellow","lys","ade")]


print "Welcome to the red/yellow yeast simulation environment."
print"Red yeast cells need adenine to grow and they release lysine."
print"Yellow yeast cells ned lysine to grow and release adenine."
print
raw_input("Please press return or enter to start the game.")

rounds = raw_input("How many rounds to you wish to play through?")
print "Starting environment", chemicals
rounds =int(rounds)
count = 0

###Game logic

for yeast in yeasts:
    yeast.setup()


while count < rounds:



    print "##########################################################"
    print "Round number",count
    print

    for yeast in yeasts:
        yeast.chem_need()


    print
    print "Chemicals in the environment",chemicals
    print

#Calls the growth code that in not yet functioning
#    for yeast in yeasts:
#        yeast.growth()

#The line below will eventually clean out dead yeast
#yeast for every yeast in the list yeasts if the yeast method returned isAlive
    yeasts = [yeast for yeast in yeasts if yeast.isAlive()]

    count = count + 1
    print "#########################################################"

print "Ending environment", chemicals

-- 
Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis
an sub cardine glacialis ursae.


More information about the Tutor mailing list