Objective

On completion of this lab you should be able to write more complex programs demonstrating flow of control (i.e. Sequence, Selection and Iteration) whilst using variables and operators.

Using Variables

  • In this step, you will work on the SomethingFishy4 exercise from lectures.

Set up your fish Sprite

  • Open the Scratch editor (if it is not open already).
  • Create a new file.
  • Add a fish sprite.
  • Delete the cat sprite.

Variables in Scratch

  • To create a variable in Scratch:

    • click on the Data button in the Scripts tab.
    • click on the Make a Variable button and give your variable a name.
  • To view the value stored in your variable, click on the checkbox beside the variable name, as shown in the screen shot below:

Viewing the value in a variable

SomethingFishy4

  • Add the following code:

SomethingFishy4

Test your program

  • Click on the green flag to run your program.
  • Does it work as expected?
    • Does your fish say "Who am I?"? The counter variable should hold a value of 0 at this stage.
    • Does your fish, if you press the a key, say "I'm a fish with a five second memory!" and pause for 5 seconds? Is the counter variable increased by 1?
    • Does your fish, if you press the c key, ignore it?

Saving your work

  • Save your work, calling the file SomethingFishy4.

Variables and Operators

  • In this step, you will work on the SomethingFishy5 exercise from lectures.

Cloning SomethingFishy4

  • As the code in this example is building on the code you wrote for SomethingFishy4, we will make a copy (i.e. clone) of the SomethingFishy4 file and call it SomethingFishy5. To do this:

    • Open SomethingFishy4 (if you haven't got it open already).
    • From the File menu, choose the Save as option.
    • When the window appears, enter SomethingFishy5 as the file name.

Refactoring SomethingFishy4 into SomethingFishy5

  • Change (i.e. refactor) the program so that it is the same as this:

SomethingFishy5

Test your program

  • Click on the green flag to run your program.
  • Does it work as expected?

    • Does your fish say "Who am I?"? The counter variable should hold a value of 0 at this stage.
    • Does your fish, Only tell the user 3 times that “I’m a fish with a five second memory”?
    • If the user asks a 4th time (i.e. they press the ‘a’ key a 4th time), does your fish thell them they have already been told 3 times? Your program should stop running here.

Saving your work

  • Save your work, ensuring the file is called SomethingFishy5.

Exercises

  • For each exercise listed below, open a new Scratch project.
  • At the end of this page, we have included the solutions. It is a good idea to jump in and have a look at them if you are having any difficulty.

Exercise 1

In this exercise, we want your sprite to count from 1 to 20.

  • Choose any sprite you fancy.
  • Create a variable called counter for this sprite only.
  • Set the value of the counter variable to 0.
  • Write a loop that will continually add 1 to this variable until the value of 20 is reached. Each time that 1 is added to the variable, your Sprite should display the value.

Exercise 2

Using a loop, get your sprite to count out all the even numbers from 1 to 20 e.g.

  • 2,
  • 4,
  • 6,
  • 8,
  • etc.

Exercise 3

Using a loop, get your sprite to say the running total of all even numbers from 1 to 20 inclusive e.g.

  • 2,
  • 6,
  • 12,
  • 20,
  • 30,
  • 42,
  • etc.

Exercise 4

Using a loop, get your SpaceShip Sprite to countdown from 10 and then blast-off.

  • Choose a SpaceShip Sprite.
  • Create a variable called countdown for this sprite only.
  • Set the value of the countdown variable to 10.
  • Get the Sprite to say Starting Countdown....
  • Write a loop that gets the Sprite to count down from 10 to 1.
  • When 1 is reached, your Sprite should say **Blast off!".
  • See can you actually get your Sprite to take off. Hint: look at the Motion scripts.

Solutions

  • The solutions for this lab are here.

  • You will notice that there are two solutions for each of the first three Exercises above. The reason is that there are multiple ways to approach a problem and each one is correct!

Challenge Exercises

  • These exercises are quite difficult and we don't expect you to be able to complete them straight away. They are challenges for you to investigate and figure out at a later date, should you wish.

  • For each challenge exercise listed below, open a new Scratch project.

  • At the bottom of the page, we have included the solution.

Challenge Exercise 1

  • In this exercise, use a loop to get your Sprite to say the lyrics to the song, Ten Green Bottles.

  • An example of the first verse is below. Keep saying this verse, reducing the bottles by 1 each time until no green bottles are hanging on the wall.

    • 10 green bottles hanging on the wall,
    • 10 green bottles hanging on the wall,
    • And if one green bottle should accidently fall,
    • There'd be 9 green bottles hanging on the wall.

Challenge Exercise 2

  • In this exercise, your Sprite should choose a random number between 1 and 20.

    • If the number is even, your Sprite should say Even and display the number.

    • Likewise, if the number is odd, it should say Odd and display the number.

  • This processing should continue until the number 7 is chosen at random.

Hint: You will need to use the mod operator for this exercise. When a number is mod by another number, the answer is the remainder. For example, 6 mod 3 is 0 whereas 7 mod 3 is 1.

Solutions

Challenge Exercise Solutions.