I am simulating a virus outbreak with the use of turtles. I have come up with the following code, my question will be after the code:
import turtle import random import time def make_population(amount): """ Creates a list representing a population with a certain amount of people. """ population =  for person in range(amount): population.append(turtle.Turtle()) for person in population: person.shape("circle") person.shapesize(0.2) return population def random_move(person): """ Makes a turtle move forward a random amount and then turn a random amount. """ person.forward(random.randint(0,20)) person.right(random.randint(-180,180)) def check_boundary(person): """ Checks if a turtle is still within the given boundaries. """ if -250 <= person.xcor() <= 250 and -250 <= person.ycor() <= 250: return person.setpos(random.randint(-200,200),random.randint(-200,200)) def infect_random(population): """ Gets a random item from the population list and turns one red """ infected = random.choice(population) infected.color("red") return infected def infect_person(person): """ Makes the turtle infected """ infected_person = person.color("red") return infected_person def simulation(amount, moves = 0): """ Simulates a virus outbreak """ border = 500 window = turtle.Screen() turtle.setup(500,500) turtle.tracer(0) population = make_population(amount) for person in population: person.penup() person.setpos(random.randint(-250,250),random.randint(-250,250)) turtle.update() infected = infect_random(population) for move in range(moves): turtle.tracer(0) for person in population: random_move(person) if person.distance(infected) < 50: infect_person(person) check_boundary(person) turtle.update() time.sleep(0.5) window.exitonclick()
So when the simulation starts I infect 1 random person, and if other turtles get close, e.g. within 50 pixels, they will get infected too and turn red. However, these newly 'infected' turtles won't infect other turtles since they are not 'infected' as compared to the initial turtle. I have tried changing it to infected = infect_person(person) but this just gives me an error. I am stuck for a while now and was wondering if there is anyone who can help. I have also thought about making two lists: population and infected_population maybe that can solve my issue but I couldn't figure out how to implement that in the rest of my code.
Thanks in advance