Lesson 2.4: Variables

Learning Objectives

Students will be able to…

  • Apply variables to track values throughout a program.

Materials/Preparation

Pacing Guide

Duration

Description

5 minutes

Welcome, attendance, bell work, announcements

15 minutes

Lecture and introduce activity

25 minutes

Guessing Game Activity

10 minutes

Debrief and wrap-up

Instructor’s Notes

Do Now Review

Do_Now Debrief

Go over all of the bugs in the Do Now program with students? (There are at least six.)

  1. In order to control Alonzo’s movement with the arrow keys, the if statements need to be in a forever loop; otherwise, the script will run once and complete. Alonzo never moves!

  2. Likewise, the script checking for the W-A-S-D keys to move the Blue Dog need to be in a loop or they will also only check for a key press one time and complete. The Blue Dog never moves, either.

  3. In Alonzo’s script, the conditional checking for the left arrow key is inside of the conditional checking for the right arrow key, so Alonzo can never move to the left.

  4. The if on edge, bounce block in Alonzo’s script is inside of the conditional checking for the left arrow key, so Alonzo will run off every edge of the screen, except the left.

  5. The fifth bug is more subtle. After the bugs preventing the movement of Alonzo and the Blue Dog have been fixed, the Blue Dog says “I got you!” when touched by Alonzo. Since the Blue Dog is supposed to be running away from Alonzo, it doesn’t make sense for the Blue Dog to say that when caught.

  6. The sixth bug is just good coding style. The Blue Dog has an unused say block in the scripting area that should be deleted.

  • Did the students have any suggestions about ways to improve the game? Here are some possible improvements they might have thought of:

    • Have a score counter that increments when the reset message is sent. (After Alonzo catches the Blue Dog.)

    • Since Alonzo starts on the left of the screen and the Blue Dog starts on the right, have Alonzo be controlled by the W-A-S-D keys and the Blue Dog by the arrow keys. That way, the control keys are on the same side of the keyboard as the sprites they are controlling.

    • Have the computer control the Blue Dog using random movements.

    • Have the computer control the Blue Dog by always moving in the opposite direction from Alonzo.

    • Have the players choose which sprite is being chased at the beginning of the game.

Lecture

Review user input, specifically the answer block

Answer Block

  • Ask students to speculate about how the answer block works.

  • Students should recognize that the block must be storing a value somehow and remembering it for later.

  • Ask students whether that type of functionality might be useful in other cases.

Introduce variables

  • Define and explain the concept of a variable: a location in memory to store a value for retrieval and use later.

  • Consider introducing variables with an interactive physical demonstration by modifying the The Box Variable Activity for your students.

    • Demonstrate creating, assigning, and accessing a variable in SNAP.

    • Point out that a variable can only hold one value at a time.

    • When a new value is assigned, the old value is lost and cannot be recovered.

    • Emphasize the importance of descriptive, readable names for variables.

    • Show that variables are independent.

    • One variable’s value can be assigned to another, as in Set Var1 to var2 Block, but changing the value of var2 later will not update the value of var1.

    Set Var1 to var2 Block

Activity

  • Briefly demonstrate the block, Pick Random 1 to 10 block which will be used in the lab.

  • Direct students to complete Guessing Game individually or in pairs.

    Pick Random 1 to 10 block

Debrief

  • Discuss and demonstrate one or more students’ submissions.

  • Ask students for commentary on usage and naming of variables throughout program.

Accommodation/Differentiation

  • Advanced students can be encouraged to implement statistics (best score, average guesses/game, number of time each secret number chosen, etc.)

  • Struggling students can be given script that completes part 1.1, and possibly also part 1.2, to get them started

  • Students who are particularly overwhelmed should focus only on parts 1.2 and 2.2

Forum discussion

Lesson 2.4 Variables (TEALS Discourse account required).