video

Lesson video

In progress...

Loading...

Hello, and welcome to lesson two of our variables in games unit.

I'm Andy, and this lesson we're going to look further at variables in programming.

As with lesson one, you'll need a web browser and access to the Scratch website.

If you'd like to save your work, you'll need an account.

If you don't have a Scratch account and you're under 13, you'll need to get a parent, carer or teacher to set you up with one.

You'll also need something to write on and something to write with.

So if you'd like to clear away any distractions that you may have, we can start.

In this lesson, you will be able to explain why a variable is used in the programme.

You'll identify a programme variable as a placeholder in memory for a single value.

You'll be able to explain that a variable has a name and a value.

And you'll be able to recognise the value of a variable can be changed.

A variable can be set and changed throughout the running of a programme.

Watch this Scratch game being run.

What values are set and changed in this game? So, what values were being set and changed while that game was running? There were two values on the screen that you could see change while the game was running.

There was the score which changed every time a goal was scored, and there was the timer which counted down the time left for the game to run.

A variable is a placeholder in the memory of a computer and it can hold one value at a time.

Each variable in a programme is named.

In a moment you're going to pause the video and you need to look at the animation and write down the name and the value of each of the variables.

Also think about why number may not be a good name for a variable.

So please pause the video now and look at the task.

So in the animation, there were two variables and two values.

The first variable was number and its value was 162.

The second variable was favourite food and its value was chocolate cake.

Keep thinking about number as a variable name and why that may not be useful to us.

You're now going to think about your favourite foods, and we're going to use those as variable values.

So, your next task is on a piece of paper write down four of your favourite foods and four numbers.

So please pause the video now.

Great, so hopefully you've got those written down now.

Here's my favourite foods, melon, chocolates, Chinese food, pizza and my numbers.

So we're going to try placing our values into this sentence.

Where are we going to put favorite_food values and number values? So I love melon and eat it five times a day.

I don't know but they are.

I love chocolate and eat it eight times a day.

So I'd like you now to try with your values, your favourite food values and your number values to make your own sentences and place them into those gaps.

So have a go, I'll just give you a moment while you try those out.

If you need to, you can pause the video.

So you have longer to try them.

As you've tried each one, you need to screw it up and throw it away because the computer doesn't remember them.

The computer uses them for the moment they're there and then they're gone like they never existed.

So in that last task, we had two variables.

We had favorite_food and number.

Favorite_food was for our favourite foods, and number was for how often we ate that food.

Can you think of a better variable name for number? Because just calling it number, we don't really know what that variable is or does what it's for.

Think about what the information was we stored in it.

It was about how often we ate a food.

I'll give you a moment to see if you can think of a better variable name.

So these are the ones I came up with, eaten, often_eaten or how often eaten with underscores in between.

I'm going to think of a different example for a moment.

Think about a classroom and where you find things.

So, which drawer do you think the pencils are in? Drawer A, drawer B, Drawer C, drawer D, drawer E, drawer F? Without looking, how would you know? If we actually put labels on the drawers like rulers, pencils, erasers, paper, pens, scissors, where would you look for the pencils now? So why is having labels more useful? Just like drawers, variables are named so that we know what they contain.

To help when programming, variable names should be short and unique and shouldn't contain any spaces, so we put an underscore sometimes if we could do with a space, home_team.

There is the variable names that could be used for scoreboard, home_team, home_score, away_score and away_team.

So your next task is you're going to be making a scoreboard in Scratch.

Each time you click on one of the flags, it will increase the score for that country by one.

So when I click on the USA flag, the score will go up by one.

And when I click on the Japan flag, their score will go up by one.

Just think for a moment, how many variables do you need for this project? Okay.

There's two countries and there's two scores, so we need two variables for those scores.

So, this is a design that you're going to follow.

When the USA flag is clicked, change that score by one, when the Japan flag is clicked, change that score by one.

So, please pause the video and you're going to have a look at making a project so that each time you click on the flags, their score changes.

And there's a starting template for you and the link is on your worksheet.

Please pause the video now.

Okay, so hopefully you've got that salted.

If not, I'm going to create mine now and you can watch me do it.

And if you had any problems with yours, you can perhaps fix it after seeing how I've done it.

So we have our project file here, we have our two sprites which are the two flags, the USA flag and the Japan flag.

And I need to make some variables, so I'm going to do that first.

Go to variables, make a variable and I need a score for each country.

So I'm going to go with USA_score for their score.

I could have gone for home score and away score but I'm going to go with that one.

And I need to make one for Japan.

So, make variable and _score.

Okay, so I've now got my two variables, I can see them here and I can see them on the screen there.

So the next thing I need to do is make it so that when you click on each sprite, the score changes.

So I need to go to events and when a sprite is clicked, I need that, so at the minute I'm on the USA sprite, and so when that flag is clicked, I need to change the score by one.

And I can see for mine, it's got Japan score, so I need to change that to USA score.

There we go.

So when the USA flag is clicked, change the USA score by one, or just try that.

That's good, that one's changing.

And if I go to the Japan flag now, I need to create the same code.

So, events when the Japan flag is clicked change and it's done it for me this time, Japan_score by one.

So I can try that one out.

And there we are.

So I have my two little bits of code and there's my scoreboard.

Okay.

So to look back over this lesson, we have looked at how variables are updated and the importance of naming them carefully.

So that's all for this lesson.

If you'd like to share your work with Oak National, you can ask a parent or carer to share it on Instagram, Facebook or Twitter, tagging @OakNational and #LearnwithOak, and I'm looking forward to seeing you next time.

That's it, thanks.