< Course F (2024)

Lesson 10: Counting with Variables

45 minutes

Overview

In this skill-building lesson, students will use variables to track a value that changes over time, like a counter. This lesson also includes a short mini-project in which students create a simple game.

Purpose

In previous lessons, students learn to use variables to store data that can be retrieved later. In this lesson, students will begin learning to modify the data stored in a variable. This key understanding of how variables work will also enable them to create more advanced projects.

CSTA K-12 Computer Science Standards (2017)
    • 1B-AP-09 - Create programs that use variables to store and modify data.
    • 1B-AP-10 - Create programs that include sequences, events, loops, and conditionals.
    • 1B-AP-15 - Test and debug (identify and fix errors) a program or algorithm to ensure it runs as intended.

Agenda

Objectives

Students will be able to:
  • Create a clicker game in Sprite Lab where sprites can be removed to score points
  • Create a variable that stores information and changes over time

Preparation

Play through the levels and review the lesson slides.

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the teachers

Teaching Guide

Warm Up (5 minutes)

Introduction

Display: Show next slide

Discuss: Which of the following types of information change over time?

  • Your first name
  • Your birthday
  • Your height
  • Your age
  • The temperature outside
  • The answer to 2+2
  • The capital of our country
  • Today’s date
  • The score in a game

Do This: Have students discuss which information would change over time and which would not. Some items might generate a larger discussion. For example, it is possible to change your first name or use a nickname. Also, over time, your height will stop changing.

Display: Show “Reflect” slide

Reflect: When is it useful to change what is stored in a variable?

Discussion goal: Students should understand that they can create a variable that stores information that either stays the same or changes over time. In a game with points, a variable is responsible for keeping track of a user’s score throughout the game. Typically, this variable starts out being set to 0 and increases by 1 for every point that the user scores. The final score is reported at the end by printing out the variable.

Teaching Tip

The goal of this discussion activity is to get students thinking about storing and modifying information using a variable. Students may recall that in previous lessons they would store information, like a word, by entering it into a prompt. In the next activities, students will learn a new way to store information as well as how to modify that information as a program runs. They should discuss when it would be necessary to change or update a variable over time.

For the discussion, decide beforehand what strategy will work best for your classroom. Some ideas include: using a T-chart, discussing in pairs or as a class.

Display: Show next slide

Remarks

The value stored in a variable can change over time.

When we start, our count variable is set to 0. After a few seconds, it increases by 1.

Review

Display: Show next slide

Do this: Use the lesson slides to review the vocabulary for this lesson. Be sure that students can recall from the lesson where they saw each of these concepts.

Remarks

When a user enters information into a prompt, the computer stores it with a variable. The prompt can also trigger an event. If the code uses a variable, the computer will look for a matching label to find the stored information.

Teaching Tip

Students shouldn’t be expected to know how variables will be used in this new context right away, so it’s okay to frame these questions as things to consider while they work.

As students move through the levels in this lesson, they will learn how to set a variable to store a number, then change that number over time. Knowing how to modify a variable’s value is the key to tracking something that changes, like the score in a game.

Main Activity (30 minutes)

Counting with Variables

Prediction (3 minutes)

Display: Show “Level 1 - Predict” slide

Do this: Have students read the provided code and make a prediction about what will happen. After pressing “Run”, provide time for students to reflect on anything they found interesting or that surprised them.

Display: Show “Level 2 - Predict” slide

Do this: Have students read the provided code and make a prediction about what will happen. After pressing “Run”, provide time for students to reflect on anything they found interesting or that surprised them.

Teaching Tip

This lesson includes two prediction puzzles: one to bridge what students have already learned and a second one to introduce the new blocks in the lesson.

Skill Building and Exploration (10 mins)

Display: Show “Level 3-5 - Skill Building” slide

Transition: Move students to their computers. Encourage students to follow the instructions for each level.

Display: Show “Level 6 - Exploration” slide

Wrap Up (10 minutes)

Reflect

Display: Show “Reflect” slide

Reflect: How did you use variables in your game?

Discussion goal: Students should understand that the variables in their game stored a value that changed over time. A variable’s value can set directly in the code, modified as a user interacts with an app, or input directly such as with a prompt.

Creative Commons License (CC BY-NC-SA 4.0).

This work is available under a Creative Commons License (CC BY-NC-SA 4.0).

If you are interested in licensing Code.org materials for commercial purposes contact us.