Building a card-trick program with my son

The first time my son Fitz wrote a line of code on purpose, he was nine, and he was annoyed with me. I had been trying to explain what a variable was, using a paper cup and three pennies as a prop. He looked at the cup, looked at the pennies, and told me, in the tone nine-year-olds reserve for adults who are being slow, that a cup holds things and a name is a name, and could we please just try it on the computer. So we opened a laptop, and I typed pennies = 3, and the annoyance turned into a small satisfied nod. That nod is the first honest reaction I have ever seen from a student of mine in twenty-odd years of teaching people programming, because it was not performed for my benefit. It was for his.

I have been thinking a lot this year about what it means to teach your own child to code. I grew up believing that programming would become as ordinary as reading and writing, and I have said so in public settings often enough that my son heard a version of it before he could read. But saying it to an audience is easy. Sitting down on a Saturday morning to do it with your own kid, while he would rather be doing almost anything else, is a different assignment.

Starting from something he already cared about

The worst way to teach a nine-year-old to code is to sit him down in front of a blank editor and tell him that today he is going to learn Python. I know this because I tried it. He was polite about it for eleven minutes and then asked if we could do something else.

What actually worked was starting from a thing he already wanted. Fitz loves magic tricks. He had a particular card trick he had seen online that involved a performer knowing which number a volunteer had picked, using a small grid of cards. He wanted to understand how it worked. I told him the trick was math, and the math was the kind a computer could help us do, and we could build our own version if he wanted. He wanted.

My son Fitz worked out the number pattern for a card-trick program we wrote together. We called the project MatheMagic. It generates cards you can print and use to reveal a number someone is thinking of. The code credits us both as authors, with a separate credit to Fitz for figuring out how to generate the numbers.

Vijit Sharma and his family introduced us to the trick. You ask someone to choose a whole number within the range covered by the cards, without telling you what it is. You show each card and ask whether their number appears on it. Whenever the answer is yes, you add the first number on that card to a running total. After the last card, your total is their number.

We spent the first few sessions not writing code at all. We drew the cards on index cards. We figured out, with a pencil, how the binary encoding worked behind the trick. Only then did we go to the laptop to generate a lot of cards quickly and print them in a nice format. The code was the tool. The trick was the thing. That order matters.

With three cards, the possible numbers are 1 through 7. The cards look like this:

CardNumbers on it
First1, 3, 5, 7
Second2, 3, 6, 7
Third4, 5, 6, 7

If someone chooses 5, they say yes to the first and third cards. You add 1 and 4. The trick uses the way numbers can be made from powers of two: 1, 2, 4, 8 and so on. Each card tells you whether its starting number belongs in that sum.

To write a generator, you need a rule for filling those cards. Fitz figured out a pattern that works as the number of cards changes. The card starting with 1 includes one number, skips one, and repeats. The card starting with 2 includes two consecutive numbers, skips two, and repeats. Starting with 4, it includes four and skips four. The same rule supplies the remaining cards, stopping at the end of the chosen range. Our JavaScript implements that pattern; the program supports up to ten cards.

Figuring out what numbers belong on the cards is substantive programming work, even before it is expressed in JavaScript. A program needs instructions precise enough to follow. Fitz supplied the pattern those instructions follow, and we coauthored the code. Calling the whole thing a lesson I gave him would leave out what he brought to it.

Knowing a programming language gives you a way to implement an idea; it doesn't tell you in advance who will contribute the idea. A child can understand something about the game you are building that deserves your attention as a collaborator. That contribution need not be new to mathematics or to the world. It needs to do real work in the project you share.

Choosing the tool for the job

I started him in Scratch because the dragging and snapping removes the typing barrier, and typing is still slow for him. Scratch is good at making loops and conditionals physical. You see the blocks nest. You see the flow. When a nine-year-old says "the green one goes inside the orange one," he is already doing the thing computer science professors are trying to teach freshmen.

We moved to Python for the card generator because Scratch did not give us a clean way to produce a printable PDF. I was honest with him about why we were switching tools. I did not pretend Python was cooler. I told him Scratch was better for one kind of job and Python was better for another, and learning which tool fits which job is half of engineering. He filed that away. A week later he asked me what tool I used at work for a particular thing, and when I told him, he asked why. It is the right question.

The version we published is in JavaScript and generates HTML tables for printing.

Letting him be wrong and stay wrong for a while

I can help Fitz put a rule into code and work through an example with him to check whether it produces the intended result. I would rather begin with what each of us can contribute and share the decisions as we work.

My instinct as a teacher, and especially as his father, is to correct errors quickly. I have had to work against that instinct. A lot of what a beginner learns happens in the twenty minutes they spend staring at an error message that does not make sense. If I jump in at minute two and fix it, I have stolen the lesson.

So I have learned to sit on my hands. When Fitz writes a loop that does not do what he expects, I ask him what he thought it would do. He explains. Then I ask him what it actually did. He reads the output. Most of the time, he finds the bug himself in the gap between those two sentences. On the occasions he does not, I give him the smallest hint that will unblock him, and no more. The lesson is the habit of comparing what you expected to what happened.

What has not worked

Long sessions do not work. Anything past forty minutes and he is done, and pushing past that has cost me trust I then had to rebuild. Praise for correct answers is less useful than I expected, and sometimes counterproductive, because it makes him anxious about the next one. Treating code as homework has been a disaster every time I have tried it. The only framing that has held is that we are building something together, and the something is his.

I have also learned that I cannot teach him the way I teach engineers at work. At work I can assume a baseline of self-direction and a willingness to read documentation. With a nine-year-old, I am the documentation, and I have to be patient enough to be read.

What I am still figuring out

I do not know yet how to hand off the teaching to someone who is not me. Eventually he should learn from people other than his father, because fathers have blind spots and because a different teacher will teach him things I cannot. I also do not know when to introduce version control, or testing, or any of the hygiene that I would insist on with a professional. Too early and it becomes bureaucracy. Too late and he forms habits he has to unlearn. I am watching for the moment where he asks, on his own, why his old working file got overwritten. That will be the right day to show him git.

What I know for certain is that the nod, the one he gave me the first time the variable did what it said it would do, is the reason to keep going. Programming, for him, is not a skill I am pushing onto his calendar. It is a small new way to make the world respond to him. That was the thing I wanted to pass on. The rest is just time and patience, and the ordinary work of being a father who shows up on Saturday mornings with a laptop and no agenda bigger than the next card trick.