In this lesson students learn how to use variables to label a number in their program or save a randomly generated value. Students begin the lesson with a very basic description of the purpose of a variable. Students then complete a level progression that reinforces the model of a variable as a way to label or name a number. Students use variables to save a random number to see that variables actually store or save their values, allowing them to use the same random number multiple times in their programs.
This lesson is the first time students will see variables in the course, and they are not expected to fully understand how variables work by its conclusion. Students should therefore leave this lesson knowing that variables are a way to label a value in their programs so that they can be reused or referenced later. In the following lesson students will be introduced to sprites, which need to be referenced by a variable.
Using variables to manipulate drawings is a surprisingly challenging skill that requires a great deal of forethought and planning. While students will use or modify many programs in this lesson, they are not expected to compose programs that use variables to modify the features of a drawing. In later lessons, students will expand their understanding of variables and more advanced ways they can be used.
Variable - A placeholder for a piece of information that can change.
Avoiding Frontloading: While this lesson begins with two resources that explain how variables work, they'll likely be more meaningful to students once they have used variables in the drawing programs. Make sure students know these resources are available, and then return to them if you like at the end of the lesson to help in sense-making.
Video: As a class watch the video introducing variables.
Review: Students can review the most important points from the video on the map level in Level 4. Students don't need to understand all of these ideas right now, but they can use this level as a reference throughout the lesson.
There's a lot to learn about variables and we'll be seeing them throughout this unit. Today we're going to see them used to help us draw pictures. The most important thing we care about today, though, is just seeing how giving a label to a value helps us write programs.
Goal: Use this discussion to assess students' mental models of a variable. You may wish to have students write their responses so you can collect them to review later. You should be looking to see primarily that they understand that variables can label or name a number so that it can be used later in their programs. While there are other properties of a variable students may have learned, this is the most important before moving on to the next lesson.
What is your own definition of a variable?
Why are variables useful in programs?
Discuss: Have students silently write their ideas before sharing in pairs and then as a whole group.
Input-Output-Store-Process: The model students learned in Unit 1 might be nice to reference here. Students are storing information in a variable which means they are saving that information in memory.
CSS Classes: If students studied CSS classes in Unit 2 then they may be familiar with the act of creating a name for something in their programs in order to be able to reference it. While the context here is different, the idea of naming remains the same.
Journal: What connections do you see between variables and what you learned about the Input-Output-Store-Process model of a computer?
In this program, the variable xPosition is used to store the x-coordinate of the circle.
Change the number that is stored into the variable (Show me where) and run the code again.
In which corner of the screen will the circle be drawn?
Hint: Click the "Show grid" box to help you find the exact coordinates on the screen.
This program uses the variable circleSize to control the width and height of a circle. If you make the circle big enough, it will fill up the entire screen, as in the picture on the right.
Change the value assigned to circleSize so that the circle takes up the entire screen.
This program has only one variable called petalSize but it uses it 8 different times. This makes it easy to quickly make lots of changes to how your picture is drawn.
Change the number assigned to petalSize so the red petals touch one another.
Try to find the size that makes the most sense. Compare your answer with someone around you.
If students use variable names that start with numbers, include spaces, or break other rules, the code may be forced into text mode the next time that they go to that level or refresh the page. To get back into block mode, students will first need to fix the problem with the variable names. Use the red error squares to see where the bugs most likely are, and once they are gone, click the "block mode" button at the top right of the workspace.
This program has multiple errors caused by bad label names. The errors prevent the program from being viewed in block mode, so the code is in text.
Find the rule each variable label violates.
Correct the errors by picking new names that follow the rules you learned in the last level.
If your program works it should draw the image on the right.
Hint: Click the arrow for a quick review of the rules.
Labels can't start with a number.
Capitalization and spelling must be exactly the same.
Variables can be assigned a random number too. This lets you save a single random value so that you can use it as many times as you want in your program.
eyeSize is assigned a random number. Unfortunately only one of the eyes is currently using it.
Change the program so that both eyes are always the same random size. Hint: you'll only need to make changes to one line of code.
Check with your teacher before tackling this challenge level.
Using variables, draw an image where many of the items rely on a common variable for size or placement. For example, draw a face with variables controlling the size and/or placement of both eyes, ears, and nostrils. Once you have your drawing completed, try changing the values of your variables to make sure that the whole image changes together.
So far we've only ever assigned a value to a variable once, but did you know you can update a variable during a program? Read through the example program we've provided, run it a few times to understand how it works, and then attempt to modify the program to draw one of the following images.