Script Variables (Teacher's Choice)
- REVISION NOTES: Polygon exercise (currently #2, #3, #4) is no longer developed adequately in earlier labs.
- Remove from this page and otherwise leave this page as is.
- Create a separate geometric graphics lab, built from U1, L2.4, L2.5 and L3, and U2L2.1 (this page) polygon problems.
- MF: I love the animation, but will students know where to find the "sides" variarble for the blocks that use it?
- GH Feedback: Here is one way to create a factorial block that takes a positive integer n and reports the value 1 × 2 × 3 × ... × (n - 1) × n. factorial block
Where is the for loop found? needs to be imported from a library - need some prompt help Import library > iteration, composition?
- We need to mention that
set
is an "assignment statement". --MF and BH
- GH Feedback 9/8/15: Overall the organization is not the best in terms of directing students to different parts of the page...the different green boxes continue the sequence from previous boxes even though they are different questions/problems - doesn't make sense.
In number 5 in the second green box, you refer to step 3 and step 5, but the lines aren't numbered...maybe add line numbers in Snap! or in the course guides?
- GH Feedback 1/12/15: For you to do, 5a is confusing (where it says step 3 and step 5)
Feedback from Al, 7/1/16:
-
These two sentences confuse me:
- You have used variables that the for block gave you. {squiral script}
- You also created variables {plural-hat-block-and-first-line-only} as inputs to blocks that you made.
- What's the difference between {\bf length} and {\bf word} in these scripts? And I think of them as \emph{inputs} to the scripts rather than variables. Have we used this ``variable'' language before?
- And I don't get what's ``extra'' about script variables.
- How about just starting with something like this: ``Sometimes you want to store information temporarily while your script is running. The script variables block lets you do that.''
- Problem 1: There's nothing for them to do after they create the variable---nothing to check or see. It can be deleted. Wait: how about this: Have it say the value of the script variable at the end of the script. If the name of the script variable is ``a'', just tack on a ``say a'' at the end of the assignment. You'll get some integer between 3 and 12. Then have a naked ``say a'' detached from the script and you'll get an error. See? ``a'' only exists inside the script.
- Problem 2: The key here is why a script variable is useful (not needed, of course\smile) in this setup. That's problem 4---and it's more important here than the other parts of 2. I'd move problem 4 to part b of problem 3. The other parts are interesting, but they are somewhat outside the main point of the page.
- Problems 5 and 6 are like fingernails on the blackboard to me. Do we really need to do this stuff about variable mutation? It's so non-functional-programming.
- Problem 6d is especially annoying---a ``guess what the teacher is thinking'' problem. The most natural way to think of 4^9 is that it's 4*4^8---as a kid, I wonder what this has to do with script variables. And, BTW, it's assumed here that exponent is a non-negative integer.
- Problem 7: As Paul can vouch, I didn't understand this. First, because the green {\bf length} works on lists, too, and I mistakingly thought it was operating on lists of words. Also, when I finally figured out what it does, it seemed like a meaningless exercise. If we can't come up with more examples like Problem 2, where a script variable is useful (if not necessary), we should stop here. I don't use such things, so it's hard to come up with better examples, but itseems that it's useful when you want to use the same value more than once and you don't care if the outside world knows about it (snarky comment: isn't that what inputs are for?). Maybe something where the pitch of a note determines how long it's played?
You have used variables that the for
block gave you. 
You also created variables
as inputs to blocks that you made.
A script variable exists only while the script is running.
Sometimes you will need extra variables to store information temporarily while your script is running. The
block lets you do that.
-
Watch the video (or read the instructions that follow it) to see how to create, name, and set the value of a script variable.
![movie showing how to choose the name of a script variable and set its initial value]()
- To create the variable, use
(in the variables tab, of course).
- To name the variable, click on the orange
a
and type the name you want.
-
To set your variable's value, use
. The down-arrow lets you select the variable you want (in this example, there's only one variable to choose).
- Then set its value.
-
This script draws polygons with a random number of sides.

This script uses the value of sides
twice, once in the repeat
block to say how many times the move
-and-turn
combination should be done, and once in computing the angle to turn
.
- Make the script and run it several times to see what it draws.
- Check your understanding: Why does it make sense to start the range of possible random numbers at 3?
- Check your understanding: Where did the formula
come from?
-
Challenge: Modify the script so that it also picks a size randomly, from 30 through 60 using only multiples of 10.
There are two ways to create a second script variable. You could use another
script variables
block

or you could extend the one block, using the right-arrow:
"U2L2-Polygons"
-
Analyzing and debugging: Instead of setting a script variable, this script just uses the
pick random
block in the two places that need the random number. Before you build and run this script, try to figure out what is likely to go wrong.
You will sometimes find it useful to compute some expression using the current value of a variable and then set the variable's value to the result. Here is one way that might look in snap.
The operation might be
or
or many others.