Skip to article frontmatterSkip to article content

Reading Code Before Writing (15 min)

Let’s practice reading and understanding NetLogo code together.

Activity 1: Code Reading

Look at this simple procedure. What do you think it does?

to setup
  clear-all
  create-turtles 100 [
    setxy random-xcor random-ycor
    set color one-of [red green blue yellow]
  ]
  reset-ticks
end
Answer (only click after discussing)

This procedure:

  1. Clears everything from the world
  2. Creates 100 turtles
  3. Places each turtle at a random location
  4. Gives each turtle a random color (red, green, blue, or yellow)
  5. Resets the tick counter to 0

Activity 2: Spot the Difference

Compare these two procedures. What’s different?

Procedure A:

to move-turtles
  ask turtles [
    forward 1
  ]
end

Procedure B:

to move-turtles
  ask turtles [
    right random 360
    forward 1  
  ]
end

What’s the difference in behavior? TTYN

Answer (only click after discussing)
  • Procedure A: All turtles move straight forward
  • Procedure B: All turtles turn randomly first, then move forward

Activity 3: Human Computer

Let’s act out NetLogo commands!

Setup:

Commands to try:

  1. create-turtles 5 - 5 students enter the “world”
  2. ask turtles [ forward 2 ] - everyone takes 2 steps forward
  3. ask turtles [ right 90 ] - everyone turns right
  4. ask turtle 0 [ set color red ] - student #0 puts on red hat
  5. ask turtles [ if color = red [ forward 3 ] ] - only red turtle moves