STEAMD Marketplace | Technology

Intro to Variables

What are variables? 

Life is full of unknowns. We don’t always know the answer to everything (it is impossible!) so our brains do some cool things to help us. For example, I can look at this room, but I may not know on the top of my head what the height of the room is. In my mind, I just think of it as the “height of the room.” For now, I can just make a guess and say the room is 3 meters high. Later I can get a ruler and measure the room to discover the real height of the room. 

In computer science when you create something to hold the place of something you may not already know, you are creating a variable. So in this scenario “height of the room” or simply “height” is my variable. 

Again, we can create variables to hold information. From now on, let’s think of that information in our head as “data.” It can hold numbers, words, or even lists! Probably the most common place you will find variables, outside of coding, is in math. 

In math, variables are used to hold the place for an unknown number. For example, I can say 2 + x = 5. In this case, x is a variable holding the place for an unknown number. Now, what plus 2 will equal 5 (3)? Now we have solved for the variable, so I would say x = 3! Computers can do the same thing! 

Another way you can use variables is in games. Say we are all playing a game of tic-tac-toe right now. We want to keep score because we want a winner. At the start of the game, your score is zero because you have not earned any points. As you earn points the score changes. You can think of a score as a variable. It is holding the value of how many points you earn and changes! 

Check it out: Check out this video from code.org to understand a little more about variables and the different types you can use when you code.

 

 

Keeping Time and Score!

Variables are a great way for us to make interactive programs with efficient code. That is super cool! But what will we actually make? Why not a simple game that can let us keep time and keep score?

Before we open Scratch, let’s look at a few blocks

In Scratch, we will see orange blocks that will allow us to create our own variables. We can name these blocks whatever we want, as long as it makes sense for the program. For example, if I will want a timer I would probably name my variable “time”. If I named it something like “avocado” I may not remember what that variable is being used for in the game!

Once we make our variable we see we have a few options set, change, show variable, and hide variable. 

Set: We can set the variable to a numeric value. For example, you may start the timer at 0 and it counts up. Maybe you set the timer to 30 and it counts down.

Change: We can change the variable by a numeric value. For the timer, if we count up we would +1 for each additional second. If we count down we would -1 for each additional second

Show and Hide: Maybe we don’t want to see the timer on the screen, so we can hide it. Or maybe we want to show the timer at the very end or at key points, we then would show it

 

Check it out: You can follow along with iamtheCODE’s tutorial video to make a simple click-based game. In the game, you will create variables to keep track of time and the score!

 

Try it out: Go to Scratch and try creating your own click-based game with variables. Can you make your game with more than one sprite?

 

 

Challenge Yourself: What happens if the timer gets to a specific time? Can you program your game to end?

 

Share Your Work: Let us know how you are doing. Share your Scratch project with iamtheCODE by tagging us on social media using the hashtag # VirtualCodeClubs