Loading...
Hi, everyone.
My name is Mrs. Jenkins.
I can't wait to learn with you today.
Welcome to today's lesson taken from the unit Using variables in programming to develop a game.
Today's lesson is called "Updating and changing values in a variable." And by the end of today's lesson, you'll be able to explain why a variable is used in a programme.
Before we get started, we have got four keywords.
These words appear throughout the lesson, so it's really important we know what they mean before we get started.
We have variable, change, set, and value.
Variable is a named piece of data stored in a computer's memory, which can be accessed and changed by a computer programme.
Change is to update the value of something in a programme.
Set is to give a variable a starting value in a programme.
And value is the data stored in a programming variable, which could be used or changed.
Keep your eyes out for those words throughout the lesson.
They were: variable, change, set, and value.
In our lesson today, "Updating and changing values in a variable," we have two learning cycles.
We have Explore how a variable's value can change and Create and name variables appropriately.
So let's start with Explore how a variable's value can change.
What is a variable? "A variable is a named piece of data stored in a computer's memory, which can be accessed and changed by a computer programme." That's a great definition.
Thank you, Andeep.
A variable can be set and changed while a programme is running.
Watch this video of a game programme being run.
What values are being set and changed in this game? "The score value is changing when goals are scored.
Score is a variable." Great answer, Izzy.
Thank you.
"The timer value is counting down.
Timer is a variable." Well done, Laura.
That's right, timer is a variable.
A variable is a placeholder stored in the memory of a computer.
This is an example of two variables.
So we have number, we have favourite food.
All variables have a name and a value.
So here we have the name number, and here is a space for the value to go.
Look at this example.
What is the name and value of each variable? So the name is number, and the value is 162.
The name of the second one is favourite food.
The value is chocolate cake.
Why do you think number is not a very helpful variable name? What did you think? Why do you think number is not a useful variable name? A, it's too short; B, it doesn't explain what the number is used for; or C, numbers can't be used in variable names.
Have a go.
How did you get on? I asked you, why do you think number is not a useful variable name? The options were: A, it's too short; B, it doesn't explain what the number is used for; or C, numbers can't be used in variable names.
The answer is B.
The name number is not a useful variable name because it doesn't explain what the number is used for.
Sofia says, "If you saw a number variable in a programme without knowing any more details, it wouldn't be useful." Andeep has replied, "You couldn't tell what the purpose of the variable is." You will get to improve this variable name later on in the lesson.
You are going to be doing an activity using sticky notes.
You will need the activity sheet and some sticky notes.
The variable favourite food stores examples of favourite food.
Choose four values for the variable called favourite food.
Write your values on the sticky notes.
So here's some examples.
I have croissant, hotdog, grapes, and pizza.
The variable number stores numbers.
Choose four values for the variable called number.
Write your values on the sticky notes.
So I have 162, 83, 27, and 15.
Arrange your sticky notes so you have a group for favourite food and another group of number values.
So here are my favourite foods.
I have croissant, hotdog, grapes, and pizza.
And for the number, I have 162, 83, 27, and 15.
Choose one favourite food and one number.
Place them on your activity sheet to make a sentence.
I love pizza and eat it 83 times a day.
Values in a variable can be changed.
The variable's value is updated to the new value.
Variables can only hold one value at a time.
True or false? Variables can hold up to three values.
Have a go.
How did you get on? I asked you: true or false, variables can hold up to three values? The answer is false.
Variables can only hold a single value at a time.
If the value of a variable is changed, the new value replaces the previous value.
When you change your values, remember to remove the previous values and destroy them.
Here is an example here with, I love croissants and eat them, or I love pizza.
So each variable each time changes and then it is replaced.
Okay, we're going to move on to our task A now.
I would like you to complete the sticky note variable activity.
So first, we're going to create four sticky notes for favourite food and four sticky notes for number.
On the activity sheet, create a sentence using the values, then record your sentence.
After that, replace the values, destroy the old values, and create a new sentence.
Repeat until you have recorded four sentences.
Have a go.
How did you get on? I asked you to complete the sticky note variable activity, creating four sticky notes for favourite food and four sticky notes for number.
Hopefully you'll have created four sentences.
Here are some of my examples.
I love pizza, and I eat it 15 times a day.
I love grapes, and I eat them 162 times a day.
I love a hot dog, and I eat it 83 times a day.
I love croissants, and I eat them 27 times a day.
For the second part of task A, I would like you to explain why number is not a useful variable name.
What would be a better name for this variable in this activity and why? Have a go.
How did you get on? I asked you to explain why number is not a useful variable name.
What would be a better name for this variable in this activity and why? Andeep has said, "The name number doesn't say what this variable is counting.
A better name for the variable could be times eaten because it shows what the value means." Great job, Andeep.
Thank you.
We are going to move on to the second learning cycle now, create and name variables appropriately.
Which drawer are the pencils in? So we've got drawer A, drawer B, drawer C, drawer D, drawer E, and drawer F.
What do you think? Why is it so hard to tell? How about now? The drawers have been relabeled: rulers, erasers, pens, pencils, paper, and scissors.
Why is this more helpful? Andeep has said to us, "Just like drawers, variables are named so you know what they contain." And Laura says, "Variable names should be short and unique." Here are some of the variable names that could be used for a scoreboard.
There is one variable for the USA score.
This variable is called home score.
There is one variable for the USA team.
This variable is called home team.
There is one variable for the Japan score.
This variable is called away score.
There is one variable for the Japan team.
This variable is called away team.
As the teams play and score points, the score variables will change.
In different games, the team names would have to change and the scores would need to be reset to zero.
Which of these is a good name for a scoreboard variable? A, home screen; B, Japan; or C, score.
Have a go.
Home score is a good name for a scoreboard variable.
Japan is not a good name for a variable as the team value can change.
Score does not specify who has scored.
Watch the animation to see the steps to create a variable in Scratch.
Okay, so we go on to Variables, we click Make a variable and we give it an appropriate name.
So home score is the one here.
And click OK.
That then creates us four new blocks all to do with home score.
Once the variable is created, additional blocks are created.
This means the variable can be added to the code.
So we have set home score to zero, change home score by one, hide variable home score, or show variable home score.
Sofia says, "This block gives a variable a starting value." And that is our set home score to zero.
This block will set the variable to be a specific value.
This code sequence will work as a setup code.
This means the score will reset at the beginning of each game.
So that is when the green flag clicked, set the home score to zero.
Sofia has explained as well that this block changes the value of the variable.
So we have change home score by one.
"These blocks can show or hide the variable on the screen." So we have hide variable home score or show variable home score.
Okay, we've got a little check-in here.
Which block will reset the code at the start of the programme? A, set home score to zero; B, show variable home score; or C, change home score by zero.
Have a go.
How did you get on? I asked you which block will reset the code at the start of the programme.
We had A, set home score to zero; B, show variable home score; or C, change home score by zero.
The answer is A, set home score to zero.
These event blocks may be useful when you create your programme.
So we have when this sprite clicked and when the green flag is clicked.
Okay, so we are going to move on to our task B now.
I would like you to open the Scratch project Scoreboard from the oak.
link/scoreboard and create a project that simulates a scoreboard in Scratch.
Create a score variable.
Each time you click on the flag sprite, the score for that country should increase by one.
When you click on the green flag, the score should reset.
Have a go.
How did you get on? I asked you to open the Scratch project Scoreboard and create a project that simulates a scoreboard in Scratch, to create a score variable, and each time you click on a flag sprite, the score for that country should increase by one.
When you click on the green flag, the score should reset.
Okay, and there is a link here to show our example game, but your game should look something like the one on the screen.
So our web link is oak.
link/scoreboard-example.
Great job, everyone.
You have done so well today and you have worked extremely hard.
Let's summarise our learning.
A variable can be set and changed while a programme is running.
Variables have a name and a value.
It's important to name variables appropriately, including an appropriate name with no spaces in it.
A variable can be set to a starting value at the start of a programme by using an event block.
Great job, everyone.
I hope to learn with you again soon.