I get a runtime error when i try to remove a fox object from a dictionary when it has starved in my simulation program, how can i fix this?
I tried to use other methods i discovered online, but i kept getting the same error.
import random
class Simulation:
def __init__(self):
self.Foxes = {}
for i in range(100): # Creates 100 foxes
self.Foxes[i] = Fox()
def Main(self):
for i in range(100): # Forages for each fox and checks for starvation
for i in self.Foxes.keys():
self.Foxes[i].Forage()
if self.Foxes[i].Hunger >= self.Foxes[i].HungerMaximum:
self.Foxes.pop(i)
print("There are " + str(len(self.Foxes)) + " foxes remaining.")
class Animal:
def __init__(self):
self.Age = 0
self.HungerMaximum = 50
self.Hunger = 0
self.ForagingChance = 20
def Forage(self):
if random.randint(1,self.ForagingChance) == self.ForagingChance:
if self.Hunger > 10:
self.Hunger = self.Hunger - 10
else:
self.Hunger = self.Hunger + 10
class Fox(Animal):
def __init__(self):
Animal.__init__(self)