Lists and Loops

In the last class we learned about variables and values. In this class we will learn about containers called Lists and about a new idea called a Loop that helps us repeat instructions easily in a computer program.

Our functions can now also draw a DODECAHEDRON which we will use to practice lists and loops.

Lists

A variable is a box or container with a name that can hold a single value. You can change the value you put in the variable at any time. A List is a box or container that you can use to hold multiple values. The list below contains five values 78,45,12,89 and 56. Each value in the list is called a list item. Variables can store either a single value or multiple values in a list.

The program to store this list is below

myNumbers = [78,45,12,89,56]

Lists can be used to store strings. The list below stores the names of sports teams in Boston.

myTeams = ["Patriots","Red Sox","Celtics","Bruins","Revolution"]

Notes

  • A list has to start with a [ and end with a ]
  • The items in a list must be separated with a ,

Loops

A loop is a set of instructions that can be repeated. A for-in loop lets us easily repeat a set of instructions for all the items in a list.

for item in listName:
    myFunction1(listItem)
    myFunction2(listItem)

Notes

  • the line that starts the for-in loop should end with a :
  • each statement that needs to be repeated should have 4 spaces in front of it

Start Minecraft and run the program cell below before you begin your tasks.


In [ ]:
import sys
sys.path.append('/home/pi/minecraft-programming')
import mcpi.block as block
import time
import drawings

Task 1

Write a program to print out the best teams in New England

  • Define a new list variable named myTeams with the list values "Patriots", "Red Sox" and "Celtics"
  • Type and run the loop program below to print the best teams one value at a time
myTeams=?
for item in myTeams:
    print(item + " is the best team in NE")
    time.sleep(2)

Nice!


In [ ]:
# Task 1 program

Task 2

The Patriots won the superbowl in 2002, 2004, 2005 and 2015.

  • Set a new list variable named wins to the list values 2002, 2004, 2005 and 2015
  • Type and run the loop program below that uses the variable named wins that prints the years the Patriots won superbowl one year at a time.
wins=?
for item in wins:
    print("The Patriots won Superbowl in " + str(item))
    time.sleep(1)

In [ ]:
# Task 2 program

Task 3

For this task you will build three circles of radius 5, 8 and 13.

  • Define a new list variable named radius with list values 5, 8 and 13
  • Type and run the loop program below to build three circles one at a time.
radius=?
blockId = block.WOOD.id
for item in radius:
    drawings.drawMyCircle(item,blockId)
    time.sleep(5)

Very nice!


In [ ]:
# Task 3 program

Task 4

For this task you will write a program to build three shapes using lists and loops. You will build a TETRAHEDRON, an OCTAHEDRON and a CUBE.

  • Define a new list variable named shapes with list values "TETRAHEDRON", "OCTAHEDRON" and "CUBE"
  • Type and run the loop program below to build the three shapes with ICE blocks
shapes=?
length=8
blockId=block.ICE.id
for item in shapes:
    drawings.drawSolid(item, length, blockId)
    time.sleep(5)

Well Done!


In [ ]:
# Task 4 program

Task 5

  • Modify the program in task 4 to add a "DODECAHEDRON" to the list of shapes being built

In [ ]:
# Task 5 program

End of Classes