Computers: a short assignment in programming

I’ve been teaching programming for two years.  I’m really not very good at teaching programming, which is why I haven’t talked about it here very much.  At first, I used JavaScript programming, because there was a great tutorial on Khanacademy.org.  But, I’m increasingly unnerved by larger questions about what school is for — and the way in which Khan Academy and other online tutoring programs are establishing a check-box grading system for everyone.  But also — it’s clear that there’s now a well-established tutorial system for JavaScript which my students have access to, and are using.  And other programming languages are not as well-established or understood in schools.  So I’ve been teaching python, using the tutorials provided by Learnpythonthehardway.org, and inventwithpython.com.  I’ve particularly enjoyed teaching my students to create the code necessary to replicate the effects of the Caesar Cipher, a simple letter-substitution code-system, using Al Swiegart’s book Hacking with Python: Codes and Ciphers, available at InventwithPython.com.

I’ve also been writing short programs, and trying to get kids to reverse engineer the structure of those programs.  This has not been entirely successful, but it’s gradually leading them (and me) into an understanding of best practices for teaching code.

Here’s the program that was our first quiz, today:

# Circle Calculator
 # by Andrew Watt
 radius = input("what's the radius of the circle? ")
 units = input("what units are you using (in double-quotes): ")
 diameter = radius * 2
 area = 3.14 * radius * radius
 circumference = 2 * 3.14 * radius
 print "the radius of the circle is %r %s." % (radius, units)
 print "the diameter of the circle is %r %s." % (diameter, units)
 print "the area of the circle is %r %s squared." % (area, units)
 print "the circumference of the circle is %r %s." % (circumference, units)
Python output
Here’s the  output from running this small python program, shown twice with different variables.

Their objective for the class was to get this program to work, while only having the output from the terminal program, that looked like this, to work from.    It took the whole class, which I wasn’t expecting; they’ve been typing in these commands for days, and seeing the results of the programming they’d done… why wasn’t it working?

Turns out, the answer had a lot to do with how programmers teach programming, i.e., for other programmers who already know a language, vs. how a teacher who’s been teaching other subjects for years teaches programming – that is, me.

My goal of the exercise was to see that they knew how to define variables, use the mathematics functions of python, and use the “print” command to show results to the user.  I also wanted them to be able to run the “input” command.

The results were… not what I wanted.  Nearly everyone got how “input” and “print” worked, and nearly everyone was able to set up the mathematics correctly.  But they couldn’t get the strings to work. Their programs looked like this:

# Circle Calculator
 # by [various students]
radius = input("what's the radius of the circle? ")
 units = input("what units are you using (in double-quotes): ")
 diameter = radius * 2
 area = 3.14 * radius * radius
 circumference = 2 * 3.14 * radius
 print radius
 print diameter
 print area
 print circumference

In other words, their programs would tell you what the results of the calculations were, but wouldn’t show what each printed variable result was.   If you didn’t know that the program was going to print in the order of radius, diameter, area, and circumference, you wouldn’t get a sense of what the results meant.

And this means that I have to do a better job of explaining how replacements work within python code.  This wasn’t something I’d spent a great deal of time thinking about, but clearly I do need to think about it.  A great deal of that challenge, it turned out, hinged on the fact that I’ve had several dozen hours to think about how programs work, and they haven’t.  I’ve learned enough of the language to become competent at making these small quiz-like programs: “can you use this function? How about this one? How about this one?”  But I hadn’t thought that this was something that needed teaching.  And clearly it does.

What did they actually know? What didn’t they know?  The answers were illuminating.

Most of the kids got how the mathematics system worked.  But variables were tricky, and the difference between variables that held strings (“text”) and variables that held numbers was confusing. They’re used to using one of those purposes for variables, not others.

This makes me think about the way that I teach Latin, though. I work kids through the structures of sentences a lot:

Marcus udus est. Claudia sicca est.
Marcus is wet. Claudia is dry.

Claudia dormit.  Marcus scribit.
Claudia is sleeping/Claudia sleeps/Claudia does sleep. Marcus is writing/Marcus writes/Marcus does write.

Marcus ambulat in villa. Claudia currit in horto.
Marcus walks in the house. Claudia is running in the garden.

A lot of what I do in Latin is help kids think about sentences as formulas that allow one to plug in different variables — a noun here, and an adjective there, change to masculine or feminine or neuter as needed.  The first two sentences, Marcus is wet, Claudia is dry, can use any pair of nouns and adjectives.  The next two sentences can use any combination of noun and verb. The next two sentences can use any combination of noun, verb and place-name.

It occurs to me that the more that I can teach my sixth graders this year to think of sentences as equations with strings or variables, the easier time I will have teaching them programming in seventh grade next year.  And that’s enough to spark ambition, really.  I think I can get them over this hurdle.

3 comments

  1. The “format” style of string interpolation might be easier to understand.

    Think of what you want the template to be.
    “How do you do, ___ ? I’m ____.”

    Here’s how to write it in Python:
    # the “blanks” have names, like filling in a form.
    “How do you do, {your_name}? I’m {my_name}”

    To plug in values, you call the format method, with name-value pairs in the parentheses.

    result = “How do you do, {your_name}? I’m {my_name}”.format(my_name=”Janet”, your_name=”Andrew”)

    And to show it:
    print( result )

    How do you do, Andrew? I’m Janet

    Here’s a variation, without giving the blanks names. format() will plug in the values you list in order.
    intro2 = “How do you do {}? I’m {}”.format(“Andrew”, “Janet” )

    The first style, with labels, is more explicit. More typing, but you know what’s going where. We’ve all filled in paperwork with “first name”, “last name”, “age”, so you can start with a familiar life experience where they’re naturally filling in strings as well as numbers.

    For an example you could give them a Mad Lib, then have them make one as an exercise.

    (I’m curious where students are likely to get hung up on this explanation. Perhaps I should write it up as a lesson and you can tweak my pedagogy.)

    -Janet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s