for each
already imported. The page should be clear that students will have to import tools if they want to use for each
in their own projects. --MF, 11/15/17In this lab, you will create tools for drawing complex designs from lists of points. To do this, you will use problem decomposition—breaking down the problem into smaller pieces.
On this page, you will create a script that lets you connect the dots (given by a list of points) to draw the letter A.
Alphie and Betsy are building a program that will take a list of points (each of which is a list of x and y coordinates) as input and connect the dots. They figure they can use it to draw pictures or graphs of data:
To make a set of starting data, they sketched the letter A on graph paper. They chose a scale that they figured would make their picture a good size on the stage.
Betsy clicks Make a variable
, names it A
, and builds .
Then she clicks that set
block to run it.
go to point
. It'll work like this x:
and y:
separately. Hmmm... go to point
block show that it expects a list as input....Yes, you can make your blocks show what type of data they expect as inputs: a number, a list, or some other type. Some languages require the data type to be indicated. In Snap! it's an option. It's not necessary but, like assigning a color to a block, it can be a helpful reminder of what the block does and what type of input it expects. You've already seen input slots of several shapes, indicating different expected data types.
point
is a list of two coordinates.go to point
block and test your block with a few points as input to make sure it does what you want it to.go to point
block for each of the points in our list.Alphie and Betsy design this new script to automate the process of going to each point.
Then they test it out.
for each
does exactly what we want. But we have a couple of bugs to fix.go to
over glide
?For Each
inputList ← [3, -1, 2, 10, -5] FOR EACH item IN inputList { IF ( (item > 0) AND (item * item > 4) ) { DISPLAY item } }
PROCEDURE Mystery (numberList, targetNumber) { counter ← 0 FOR EACH number IN numberList { IF (number > targetNumber) { counter ← counter + 1 } } RETURN (counter) } myMathGrades ← [100, 80, 90, 80, 60, 100, 50, 100] Mystery(myMathGrades, 60)