Script Variables

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.

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 script variables block lets you do that.

  1. 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

    1. To create the variable, use script variables (in the variables tab, of course).
    2. To name the variable, click on the orange a and type the name you want.
    3. To set your variable's value, use set. The down-arrow lets you select the variable you want (in this example, there's only one variable to choose).
    4. Then set its value.
  2. This script draws polygons with a random number of sides.

    random polygon script

    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.
    1. Make the script and run it several times to see what it draws.
    2. Check your understanding: Why does it make sense to start the range of possible random numbers at 3?
    3. Check your understanding: Where did the formula 360/sides come from?
  3. 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 script-variables-sides-script-variables-size or you could extend the one block, using the right-arrow adding a second script variable
    "U2Lab2Polygons"save your work as U2Lab2Polygons
  4. 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. buggy random polygon

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.

Why does this font look so different? Is this from Snap!? --MF
set-result-to-(operation-on-result)

The operation might be join or times or many others.

  1. Here is one example.
    example-set-result-to-5-say-result-set-result-to-result-x-2-say-result
    1. Build it, try it, and figure out why say-(result)-for-2-secs says one thing in step 3 and a different thing in step 5.
    2. Change the 5 to a 7 and predict what this script will do.
  2. This script leads up to a more useful example.
    power--set-result-to-1-repeat-2-(set-result-to-result-times-5)-say-result
    1. Build it, try it, and figure out how it does what it does.
    2. Change the times 5 to times 3 and predict what it will do.
    3. Then change the repeat 2 to repeat 3.
    4. Build a reporter block named raise-base-to-the-power-of-exponent.
      The script you created should help. Put the variables (base) and (exponent) in the right places inside that script.
      Your block should work like this:
      raise 2 to the power 3 (result = 8) raise 2 to the power 5 (result = 32) raise 3 to the power 4 (result = 81) raise 2 to the power 1 (result = 2)
      Make sure it also works for this case: raise 5 to the power 0 (result = 1)
      Check (as always!) to make sure it works as you expect.
  3. Here is another example.

    vowel-remover-script, first line: script variables (result)(somewords); second line: set result to (empty slot); third line: set somewords to (any sentence you like); fourth line: for i = 1 to length of (somewords) {if not vowel? letter i of somewords, set result to (join result letter i of somewords)}; fifth line: say result

    1. Build it, try it, and figure out how it does what it does.
    2. Change the code so that it puts in hyphens (-) where the vowels used to be.
    3. Change the code so that it counts the vowels, counts all the letters (not including spaces), and ends with
      say-(vowels)-for-2-secs-say(total-letters)-for-2-secs-say((join-words-(this-passage-is)(vowels-divided-by-total-letters)-percent-vowels
      If you like, compare the results you get from similar length sentences in two different languages. You may get very interesting results.
Save your work