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.
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 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
In [ ]:
import sys
sys.path.append('/home/pi/minecraft-programming')
import mcpi.block as block
import time
import drawings
Write a program to print out the best teams in New England
myTeams=?
for item in myTeams:
print(item + " is the best team in NE")
time.sleep(2)
Nice!
In [ ]:
# Task 1 program
The Patriots won the superbowl in 2002, 2004, 2005 and 2015.
wins=?
for item in wins:
print("The Patriots won Superbowl in " + str(item))
time.sleep(1)
In [ ]:
# Task 2 program
For this task you will build three circles of radius 5, 8 and 13.
radius=?
blockId = block.WOOD.id
for item in radius:
drawings.drawMyCircle(item,blockId)
time.sleep(5)
Very nice!
In [ ]:
# Task 3 program
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.
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
In [ ]:
# Task 5 program