Script Variables
- MF: I worry that the mathematics required on this page would be non-trivial for our students. What if they haven't had any geometry?
- 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?
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.
- 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
"U2Lab2Polygons"
-
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.