jump to navigation

Teaching my Son Python March 18, 2008

Posted by PythonGuy in Beginning Programming, Education, Python.
add a comment

I home school my son. Why? Well, that’s a whole other ball of wax.

One of the opportunities that home schooling affords is I get to choose the curriculum. Part of that curriculum includes learning to program. Whether my son decides to be a lawyer, a plumber, or a high-energy particle physicist, I expect that at least the understanding of how to program will come in handy.

I started to learn to program when I was just a bit older than him on an old Commodore 64 with BASIC. He just turned 7. I had tried some experiments earlier to see if he could “get” programming, at least the most fundamental concepts last year, and I had prodded lightly over the year. But this time, he seems to start to “get” it.

At first, he used it like a calculator. You type in the numbers to the Idle prompt, and out comes the same number. You put in a +, -, *, or / and it does all the hard work for you. (He doesn’t know about decimals yet—although he knows how to add and subtract money including cents.)

What really fascinated him was this short program, written in Python 3.0:

import random

def guess_my_number():
  number = random.randint(1,1000)
  while True:
    guess = int(input("Guess my number: "))
    if guess > number:
      print("You're too high!")
    elif guess < number:
      print("You're too low!")
    else:
      print("You got it! You must be very smart.")
      break

This is a very simple program, but it plays a game with my son that requires him to think logically and almost run a similar program, almost as easy to write:

import random

def let_me_guess():
  high = 1000
  low = 1
  while True:
    guess = random.randint(low, high)
    answer = input("Is it %d? (yes/low/high) " % guess)
    if answer == 'yes':
       print("That was fun!")
       break
    elif answer == 'low':
       low = guess + 1
    elif answer == 'high':
      high = guess - 1
    else:
      print("I didn't understand you.")

Now, I didn’t write the programs perfectly the first time, but thanks to Idle it was a trivial matter to find and fix bugs. I am not entirely certain that the above two programs work—I typed them from memory. But if they don’t, it would be a good learning experience to make them work. So he got to see part of the development process—write, rum debug, repeat.

Having the computer be just as smart as dad at guessing a number was an interesting revelation to my son. I guess something clicked in his head that there was something magical happening inside that strange metal box, something that wasn’t completely beyond explanation and something he could harness. After seeing the computer guess his number, he began playing guess_my_number() again and again. In that process, he discovered a bug. If you type in a very large number, then the whole thing throws a ValueError. He actually made it a game of seeing what input caused the error and what didn’t.

I tried to play with the debugger, but I hardly ever use it and it didn’t seem to do what I wanted anyway. It would’ve been neat to show my son how the program actually ran, but truthfully, if you don’t understand the concepts, it will still be mysterious.

It also reminded me about how much “art” (art as in technical experience, know-how) goes into programming. You have to know how to use the keyboard, run programs, debug programs, use a text editor, and a whole number of things. If all of these pieces don’t come together, if you’re deficient in one area, you can’t program. It would be nice if we could make the programming task even simpler by removing some of these dependencies—dependencies with really didn’t exist in the C64 era—but alas, I don’t see a way to do it and still write “real” programs.