Feb 8, 2012

Programming as Problem Solving

Computer programming provides an ideal environment for young students to develop the cognitive skills required to solve a problem gradually through trial and error... and error, and error, and error.

At a New York City public school with a focus on science and math, I observed ninth grade students in a class called Engineering, devoted mostly to introductory computer programming. Students had spent much of the beginning of the year working in Alice, a simple-to-learn 3D programming environment, but had moved on to a text-based, open-source language called Python. Python is rapidly developing a huge following in both the programming and teaching worlds, in part because of its simplicity, readability, and diverse and dynamic library of resources.

Many of the students in the class were working on a coding a guessing game in Python, an assignment written by Tim Wilson as part of a public library of Python-related materials.  In this game, the computer selects a random number between 1 and 100, and the user enters integers until they guess the number correctly. I've included below a few examples of observations I made while watching students make their way through this challenge:

In developing a method of comparing a user's guess to the computer's selection, one student had written a guess test consisting of the following code:

    if guess = computernum:
        print 'You got it!'

This student was busy running the program and trying out different numbers, but was getting increasingly frustrated that the computer just sat there silent whenever he entered an incorrect guess. 

Another student was trying to print the number that the computer had guessed, to make it easier to debug her guess-comparison code. Another student tried to help by suggesting that she include the code "print computerchoice" but any time she ran the program she got an error message:

Traceback (most recent call last):
     File "/Users/Documents/Python/guessinggame.py", line 10, in <module>
        print computerchoice
NameError: name 'computerchoice' is not defined

Another student had successfully completed the necessary code for making and checking a guess, and he was trying to expand his code to allow for multiple user guesses, in sequence. He copies the section of his code that checks the guess and pastes it below the first "guess-checking" section, but he is puzzled when he sees the computer's emphatically repetitive response:

What is your guess?56
ha ha you got it wrong
ha ha you got it wrong 

An introductory course in programming is, above all, a course in problem solving. The world of programming plays by consistent rules, and solving problems within this environment means learning those rules. Large tasks can be broken down into discrete, testable chunks, and incremental progress is rewarded with immediate feedback. Creativity and collaboration go hand-in-hand, and successful and unsuccessful approaches are identifiable by the program they produce. Any differences of opinion on what approaches might work are best resolved by simply trying things out - as recently quoted by our nation's youngest billionaire, "Code wins arguments."

We physics teachers like to paint the universe as a place that operates by a few simple relationships, but demonstrating the simplicity of these relationships can sometimes be a little complicated. When faced with a complex problem, developing a successful solution takes a organized approach based in the assumption that a system is doing what it's doing for a reason. Rather than simply saying, "it's not working..." a successful problem solver admits "It's working... It's just not doing what I want it to do. Why is it working this way and how can I change it?" Programming is an ideal venue for practicing this skill. As each of the issues I described above were resolved, students were rewarded with a more sophisticated understanding of the "unsuccessful" program they'd written, as well as the modification that brought the program closer to completing the task at hand.

Later in the course, students are exposed to a 3D graphics module called Visual, which allows for fluid 3D programming within Python, or as it's referred to when used with this module, VPython. All students at this school take physics in ninth grade as well as engineering, and one of the later assignments in the course is to use VPython to program a 3D simulation of a projectile moving in two dimensions. What better way to help students arrive at an understanding of the rules nature uses to determine the position of a projectile than to ask students to code these rules themselves?

Leave a Reply