Dark theme

Containers


Let's start by creating ourselves a list for agents.

At the top of our code, after the import, add in the following:

agents = []

This creates a new empty list we can add sets of coordinates to. We will add several sets of coordinates to this one list, so we only need to do this command once.

Then, after the making of y0 and x0, add:

agents.append([y0,x0])

This adds our first set of coordinates. Notice the extra square brackets. Here we're adding the coordinates as a list inside another list. We have a two dimensional dataset, in which the first dimension agents[0] represents each agent, and the second dimension is the two coordinates, so agents[0][0] is the y coordinate and agents[0][1] is the x coordinate. As we only have one set of coordinates so far, if you print(agents) you should see something like this (though the numbers will be different):

[[34,22]]

Go through and replace all the other uses of y0 with indexed list references, i.e. agents[0][0] and x0 with agents[0][1] (all, except the first assignments). Check it works.


Now, about those first two assignments. At the moment we have:

y0 = random.randint(0,99)
x0 = random.randint(0,99)
agents = []
agents.append([y0,x0])

It seems pointless to create the y0 and x0 coordinate variables for this short period. Can you think of a way to get rid of them?


  1. Introduction
  2. This page
  3. Making lists II <-- Next
  4. Using functions