Time for action - set user-defined variables

We need to know if our script works, so let's define the variables to values other than zero:

  1. Add a control block to our calculation script. Snap the when space bar pressed block onto the top of the set newAmount to startAmount block.
  2. Now, test the work you did in the hero exercise. Click the flag to reset the values of each variable so that count equals 0, days equals 7, startAmount equals 1, and newAmount equals 0.
  3. Press the Space bar to calculate the amount of money you would have if you started with one dollar and doubled it every day for seven days. The value in newAmount is 64.
  4. Let's give the user the ability to set a starting amount. Find the startAmount monitor on the screen and double-click it to display a slider. You may need to drag the monitor block around the screen if it's covered up by another monitor.
  5. We want to make our numbers are manageable, so let's limit the range of numbers we accept. Right-click on the startAmount monitor and choose set slider min and max.
  6. In the Slider range dialog box, enter 1 for the min value and 5 for the Max value. Click OK.
    Time for action - set user-defined variables
  7. Now, drag the slider slowly to the right and note that, as you move, the value increases, but it does not exceed a maximum value of 5 or a minimum value of 1.
  8. Run the script several times using several values for newAmount, and test the results.
Time for action - set user-defined variables

As you run the script, don't forget to click the flag to reset your calculations. Enter a new startAmount and press the Space bar to calculate the value.

What just happened?

If you look at the monitor blocks on the stage, you'll notice every block has a value. Did you notice a pattern in your calculation? If not, rerun the script using the values 1, 2, and 4. If we double the startAmount, the newAmount doubles as well.

Clicking the flag offers an easy way to reset our calculation, and I'd be willing to bet that you tried to press the Space bar once or twice without clicking the flag first. You'll note that nothing happened. That's because the value of count already equaled the value of days.

The slider control we added to the startAmount monitor provides a user-friendly way to set an initial number and interact with the program. The bigger the number in startAmount, the bigger the final value in newAmount will be.

For example, if we start with four chicken feet, we would end up with 256 chicken feet after seven days. That's a lot of scratchin'.

Tip

The slider can also accept decimal values to the tenths position. To enter decimals, specify the decimal value in the min and max values of the set slider min and max control (e.g., Min: 0.1 and Max: 5.0).

Let's inspect our calculation using the single stepping feature of Scratch.

Start single stepping

We've already discussed the single stepping feature in Scratch, but single stepping through our calculations is a great way to understand the code other people have written. Essentially, you've been duplicating code at my direction, but that doesn't mean it makes sense.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.229.143