Intermediate Variables
Contents
Introduction
A variable is a placeholder for a piece of information that may change. You’ll probably recognize variables from many popular children’s songs that tend to repeat the same verse. Remember the song ‘Old MacDonald’?
How long do you think it would take if we had to write verses for every farm animal we can think of? Both the animal and the animal noise could be placeholders for their own names – for example: animalName and animalNoise. These placeholders are like variables, because they store a value like sheep or baa, which we can fill in later.
- Old MacDonald had a farm
- E-I-E-I-O
- And on his farm he had a animalName
- E-I-E-I-O
- With a animalNoise animalNoise here
- And a animalNoise animalNoise there
- Here a animalNoise, there a animalNoise
- Everywhere a animalNoise animalNoise
- Old MacDonald had a farm
- E-I-E-I-O
What is it?
A variable is a placeholder for a piece of information than can change.
A value is a piece of information that a variable represents.
Abstraction is pulling out specific differences to make one solution work for multiple problems.
Activities
Artist Variables
Explore the creation of repetitive designs using variables in the Artist Variables puzzle on Code.org. You’ll learn how to use variables as a way to quickly change many values at one time. Not only will you be dealing with the looping of designs and repetition of angles, you’ll also be doing math on variables. So, you might find it helpful to have scratch paper and pencil on hand to figure out variables as you go.
Play Lab Variables
In the Play Lab: Variables puzzle on Code.org, you’ll have the opportunity to play with variables in a situation that shows just how useful they can be. You’ll edit games to give yourself the advantage and make your characters more powerful using variables as parameters.
Robot Variables
As a team, take three envelopes and give them the following labels: 1) robotName, 2) numUnitsTall and 3) purpose. Now, you’ll need to divide your team into pairs. Together, think about a robot. What is it supposed to do? What does it look like? Draw your robot in the space below.
When you and your partner are done answer the three questions below on slips of paper and place them inside the corresponding envelope.
- My robot’s name is _________________________. (robotName)
- My robot’s height is _________________________. (numUnitsTall)
- My robot’s primary purpose is __________________. (purpose)
Each pair should then pick one of each envelope randomly. Plug in the variables from the envelopes you selected and form the following sentence: My robot’s name is robotName , it is numUnitsTall tall, and its purpose is purpose. Take turns reading your sentences out loud to the rest of team. What is different about each sentence? What is the same?
Our envelopes are like variables , and the information in them are what we call our value . The value is a piece of information that our variable represents. The value of a variable can change. For example, we can change our robot by taking something out of the envelope and putting something else in.
Abstraction with Mad Glibs
Abstraction is one of the most important skills for a computer scientist to understand. It simplifies problems and prevents unnecessary repetition. Good coders use abstraction just about every time they create a program. In this activity, you’ll analyze stories for differences so that you can abstract them away.
The paragraph below started as a specific story about one thing, but we used abstraction to turn some of the specific words into blanks - and now the story can be about lots of things. Work in pairs to create the following Mad Glibs story. One person can come up with words for the variables in parenthesis and the other person can write their responses. When you’re done, take turns reading your stories out loud.
- Yesterday, ___(person)______ and I went to the park. On our way to the ____(parkSize)______
- park, we saw a ___(color)___________ ______(animal)__________ on a bike. We also saw
- ______(color)____________ balloons tied to a ___(object)________. Once we got to the
- _____(parkSize)_______ park, the sky turned _____(color2)______. It started to :____(weather1)__
- and ______(weather2)________. ____(person)_______ and I ____(action)_________ all the way
- home. Tomorrow we will go to the ____(place)___________ and hope it doesn’t ___(weather1)____.
Remember - abstraction is just pulling out differences to make one solution work for multiple problems. You’ll try this in the next part of the activity by doing the opposite - finding out what the stories have in common and making a template that will work for both stories.
Look at these stories and figure out which words are supposed to be blanks, then recreate the template at the bottom of the page.
Story 1
Early last year, my mom gave me an old skateboard. She told me about the days when she would ride it from her school in her hometown. I tried to ride it once, but tripped over my shoelaces. It didn’t take long before I decided that it was best to leave the skateboarding to my mom.
Story 2
Sometime last year, my mom told me an old story. She told me about the days when she would hear it from her father in her childhood. I tried to tell it once, but tripped over my words. It didn’t take long before I decided that it was best to leave the storytelling to my mom.