Top ways students struggle to code with variables

Jess Norris
Oct 31, 2018 · 5 min read
Learning to Code sometimes feel like this — Photo by Logan Fisher on Unsplash

We’ve been running competitions like the NCSS Challenge, or the upcoming CodeQuest competition for many years, and in that time we’ve seen many students struggle to understand the concept of a variable.

This is one of the first concepts we introduce, so it’s not surprising that students hit the wall pretty early on. How can we help them?

What is a variable, anyway?

A variable allows a programmer to associate a name with a value — mostly where the value is expected to change. It’s a way to label data, to make it easier to refer to later in the code.

Every time we refer to the variable name in our code, the computer substitutes the current value that is stored.

Calling Elvis … anyone home?

It’s very common for students to struggle with this concept, when they try writing even the simplest code that uses a variable. Here is an example Python snippet which will print out the number of lemons at a lemonade stand, according to what the user — the stand owner — enters:

This code has a couple of things going on:

  • The number of lemons is set in the first line,
  • then the value is read, in the second.

The key concept here is that the value stored in the num_lemons variable can change, according to what the user types. We could even answer with a completely unexpected value, like “no” instead of a number:

There are no lemons at the lemonade stand!

Spot misunderstandings from the code

Get to the source of the problem — Photo by John Schnobrich on Unsplash

Misunderstanding the concept of a variable can manifest itself in student code with startling variety, even in a two-line program like the example above.

Let’s have a look at some of the most common ways we’ve seen crop up over the years.

First up, it is amazingly common for students to name the variable after the first value that they expect to test:

Does this student realise the variable can change its value? Maybe not — they’ve associated a particular value in their mental model of the variable, which can lead to later confusion.

Here’s a related example where the student has hard-coded their output based on that first example:

This code will always produce the same output, no matter what the user enters.

And here again: the student knows the user will provide the number of lemons, but they don’t know what to do with the answer (store it in a variable):

What about not realising that a variable is needed in the first place?

This student is trying to make their program produce different output, according to two different example user inputs (10, and then later, 24).

The missing link here is that they should only print one line of output, and that output needs to change. They need a variable to store the number of lemons, since it is different each time.

Help them understand

So how can you help your students who are struggling in this way? Here’s an offline activity that is appropriate for a small group of students that can help them grasp this important concept.

You will need:

  • Something physical you can flip over — a piece of two-sided laminate, or a square of heavy card.
  • An erasable marker / pencil for writing on the front and back.

In this activity, each student pretends to be part of the computer, and they each become a programming command. The flip-board represents the variable inside the computer.

Let’s give some context to this activity, to make it easier for students to relate:

You’ve found a great recipe for pear and chocolate muffins in Granddad’s notebook. The recipe tells you how many pears you will need for a batch of 12. Let’s get baking!

Photo by Clem Onojeghuo on Unsplash

☛ Write num_pears onto one side of the flip-board. This is the name of the variable. On the back, write a large 2. This represents the number of pears published in the recipe.

Set up the flip-board

☛ Give each student a command that will use the variable in some way. Here are some ideas that work well for most age groups:

  • Print the recipe to share — print(num_pears + ‘pears makes 12’)
  • Double the batch! — num_pears × 2
  • Needs more flavour! — num_pears + 1
  • Make enough for a mega cake stall — num_pears × 10

☛ Get the students to “run” their command:

  1. Give them the flip-board, name side up.
  2. They start work on their command, and when they get to num_pears they flip the board over, to read the value.
  3. They tell the group what their command should do, or what the output should be.

Next, ask the students to swap their commands amongst themselves. Whilst they do this, change the value on the flip-board to 4.

Repeat the activity so that the students can see how the commands produce different results, now that the value has changed.

It’s helpful to repeat this a few times a few times more, giving each student the chance to set the variable, by writing it onto the flip-board.

At the end of the activity, ask the students what name they would choose for the variable if the recipe was for apples instead of pears. Do they agree?

You might have a chance to try this out soon, if your students are participating in our upcoming CodeQuest competition.

Maybe you already tried something like this before? We’d love to hear what your experiences are like! Give us a clap if you think this would be useful in your classroom.

Grok Learning

Articles about coding by the team at Grok Learning &…

Jess Norris

Written by

Software Developer at Grok Learning

Grok Learning

Articles about coding by the team at Grok Learning & teacher friends.

More From Medium

More on Teaching from Grok Learning

More on Teaching from Grok Learning

Grok, Micro:bits & LED’s

More on Edtech from Grok Learning

More on Edtech from Grok Learning

JS on Grok creates puzzle nostalgia

More on Programming from Grok Learning

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade