Tue 23 Jun 2009
Programming for Six-Year-Olds
Posted at 23:41 -0500 (last edited: 23 Jun 2009, 23:20)
I've wanted to introduce my son to programming for a while, but I've been concerned about the approach. I tend to get hung on the details and design of my software, and that slows me down from actually writing lines of code. For him, I wanted to show programming as something fun and immediate, that doesn't require a lot of stress up front.
Tonight, I think I hit on a great introduction…
At bed time, he and I usually spend some time with my laptop, watching videos. In fact, we just finished the last episode of Design Squad, which was fantastic. Tonight, I opened up the Python interpreter, and assembled a simple animal class before our time together.
>>> class Animal: ... def speak(self): ... print "I am an animal." ... def eat(self, food): ... print "Yum! I love to eat %s!" % food ...
After tonight's videos, I switched to the window, and told him I wanted to make some imaginary animals. Pointing to the text, I said that I imagined that animals could speak and eat, and then I asked him what kind of animal he would like to make.
"A pig!" he said.
>>> pig = Animal() >>> pig.speak() I am an animal.
"Hmmm. Is that what a pig says?" I asked.
"No", he replied.
Before worrying too much about how to fix the pig's voice, we decided to feed it something, so first we tried some corn.
>>> pig.eat("corn")
Yum! I love to eat corn!
And then some bread…
>>> pig.eat("bread")
Yum! I love to eat bread!
…at which point I said the a pig would probably eat anything.
>>> pig.eat("anything")
Yum! I love to eat anything!
My son was enjoying this, so I decided it was time to fix the pig's sounds. "Are pigs a type of animal?" I asked.
"Yes."
>>> class Pig(Animal):
"What does a pig say?"
"Oink!"
... def speak(self): ... print "Oink! Oink!" ...
I didn't explain what I was doing as I was typing, but he was watching; as the title indicates, he's six, and is an excellent reader (as any father would say, I know).
I said, "Let's make the pig a pig this time!"
>>> pig = Pig() >>> pig.speak() Oink! Oink!
"Oink! Oink!" I heard from beside me. Then we checked to make sure the pig still liked to eat anything.
>>> pig.eat("corn")
Yum! I love to eat corn!
>>> pig.eat("anything")
Yum! I love to eat anything!
With the pig acting more piggish, I thought it was time to get another animal in here. I began building a dog class.
"What does a dog say?"
"Ruff!" he said.
>>> class Dog(Animal): ... def speak(self): ... print "Ruff! Ruff!"
Taking things a bit further, I asked "Does a dog eat anything?" This got the expected "no" response, and after some prompting, he suggested that a dog would eat dog food, and agreed that a dog would eat meat. With this use case in mind, I coded the logic.
... def eat(self, food): ... if food == "dog food" or food == "meat": ... print "Yum! I love to eat %s!" % food ... else: ... print "Yuck! I don't like to eat %s!" % food ...
A very cool thing happened while I was typing the if
statement; at the begining, I heard him say to himself: "If food equal
dog food…" and trail off.
I picked this up and completed it as "or food equals meat, then say yum! Or else, say yuck!". I didn't bother explaining the print syntax, or what was a string, etc. These are the details I don't want him to worry about, until he's having fun and needs to know.
This time, he decided to name the dog after our belated German Shepard, Rio. I was happy, because that's a better example of an instance, and because Rio was a good dog.
>>> Rio = Dog() >>> Rio.speak() Ruff! Ruff!
I was sweating the next part a little bit, since I was coding everything on the fly, and I was already onto my third class, and I hadn't even made a mistake in indentation. Testing Rio's food preferences risked some cognitive dissonance if he developed a fondness for lettuce.
>>> Rio.eat("dog food")
Yum! I love to eat dog food!
>>> Rio.eat("carrots")
Yuck! I don't like to eat carrots!
Like I said, a good dog. Very reliable, especially when it came to vegetables.
The last few minutes devolved into me feeding Rio and the pig different things, which demonstrated how simple humor is at six. It's amazing to hear a kid laugh at a print statement representing a dog that doesn't like to eat hamburgers.
>>> Rio.eat("bunny")
Yuck! I don't like to eat bunny!
>>> pig.eat("bunny")
Yum! I love to eat bunny!
>>> Rio.eat("oranges")
Yuck! I don't like to eat oranges!
>>> Rio.eat("hamburgers")
Yuck! I don't like to eat hamburgers!
>>> pig.eat("hamburgers")
Yum! I love to eat hamburgers!
>>> pig.eat("bunny")
Yum! I love to eat bunny!
>>> pig.eat("carrots")
Yum! I love to eat carrots!
>>> Rio.eat("bread")
Yuck! I don't like to eat bread!
>>>
After all this, we talked for a little bit how my imaginary animals
weren't doing a great job acting like the real ones, and how these animals
may have to inspect their food more closely. (I'm just warming him up
for an introduction to isinstance--he's going to be so
disappointed when we get to C++.)
All in all, it was a very fun 10 minutes, and I introduced classes, inheritance, method overloading, conditional statements and modeling. When I went downstairs and described all of this to my wife, she was amused, but demanded that all classes of animals be made to like vegetables--even Rio.
Topics: