I know this is a very simple question, but unfortunately I don’t know enough to search effectively for an answer. Any answers or links to things I should already know would be greatly appreciated.
What I am trying to do is make an environment in Python where I have a bunch of turtles running around doing various things (it’s basically like StarLogo from MIT).
class Turtle:
def __init__(self,x,y):
self.xpos = float(x)
self.ypos = float(y)
self.head = 0.0
numTurtles = 4
for i in range(numTurtles):
...
MyTurtles = [...]
Each turtle has an x position, a y position, and a heading (in radians). All I want to do is create them and tell them where to stand (their x and y coordinates), and then put all the turtles into a list so that later I can tell the whole group to take a certain action.
I want to be able to change the number of turtles, otherwise I would just call them [a,b,c,d]. But I figure there has to be a better way.
You should add the turtles to the list directly while executing the loop, for example
It’s often also possible to write this as a “list comprehension”: