Coding with Variables in Scratch
Create code in Scratch that mimics the throwing of a die using a random number generator, selection, a variable, and output. The instructions below will help you to write the code.
Roll the Dice - Instructions
Before you start this activity, you will need to be familiar with Scratch. It would also be helpful to know how to declare and use a variable.
Create a variable by clicking on the ‘Make a variable’ button in the ‘Variables’ code section. You can name it anything you like. In this example, it has been called ‘Die Value’
After that, add a block that will start your code running (e.g. pressing the green flag), followed by a block that sets the value of your variable to a random value between 1 and 6
Add an if...else block to your code. If the random number equals 6, the sprite should say "You can start the game!" for 2 seconds. Otherwise it should say "Too bad - try again!" for 2 seconds. Test run your code to check that it behaves as expected.
Add a loop to your code that will loop until the random number is a 6. You will need to test this step carefully and debug any unexpected behaviour!
Hint: adding this block at the start of your code will help!
If you get stuck, you can find an example solution in this Scratch project: