AttributeError: 'NoneType' object has no attribute 'is_alive'

870 Views Asked by At

this is my first time asking on StackOverflow so if there is anything then please understand for me. I'm trying to create a program for my lab that simulates the growth pattern of a colony of bacteria, it is to practice Class and List. This is what I am having

class Bacteria:
     def __init__(self, chance, life_span):
         self.chance = chance
         self.life_span = randint(1, life_span)

     def live_a_d(self):
         self.life_span = self.life_span - 1
         if (self.life_span > 0):
             value = randint(1, 100)
             if value < self.chance:
                 new_bacteria = Bacteria(self.chance, self.life_span)
                 return new_bacteria
             else:
                 pass


     def is_alive(self):
         if (self.life_span > 0):
             return True
         else:
             return False

class Colony:
     def __init__(self, seed):
         self.seed = seed
         self.time = 0       

     def live_a_day(self, printDailyReport = True):
         new_member = []
         old_size = len(self.seed)

         for i in range(old_size):
             if (self.seed[i].is_alive):
                 new_bacteria = self.seed[i].live_a_d()
                 new_member.append(new_bacteria)
             else:
                 self.seed.remove(self.seed[i])
    
     self.time += 1
     self.seed.extend(new_member)
     new_size = len(self.seed)
     new = len(new_member)
     print("Day %5d Colony Size %6d New Members %6d Expired Members %6d" % (self.time, new_size, new, 0))

def main():
     bacteria = Bacteria(60, 10)
     starter = [bacteria] * 10
     colony = Colony(starter)

Everytime I run, it said AttributeError: 'NoneType' object has no attribute 'is_alive' but when I run

def main():
     colony.seed[0].live_a_day()
     print(colony.seed[0].life_span)

The code runs.

I have tried get and set from others post but nothing seems to work and I don't know where to start to fixing my code.

Thank you for spending your time on my post.

0

There are 0 best solutions below