In a comment to Alfred Thompson’s blog post on “Any monkey can code” I talked briefly about a simple coding problem “given a list of integers find the sum of the evens (or the odds or the integers divisible by 3 or whatever). The Python book I am using has a lot of problems like this scattered through it. How difficult are these problems? Anyone with experience in programming will think the problem is pretty trivial. Depending on the language it is 6 lines of code. Here is my solution in Python. (There may be a better solution but this is my solution so all you programming brain surgeons out there, lighten up.)

- ints = (1,2,3,4,5,6,7)
- n = 0
- for i in ints:
- if i % 2 == 0:
- n = n + i
- print(n)

I took me maybe 5 minutes to come up with this solution, mostly because I am a slow typist. Now let’s look at what is involved in this little program and how a kid would have problems coming up with a solution.

There are a lot of simple concepts in this little program that all have to work together to get a result. Starting at line 1 here I am making a single variable equal to a list with bunch of numbers in it. Kids know what a variable is; they have been dealing with them in math for years. That still does not mean they have a firm grasp of the idea, and this variable name is more than one character, not allowable in the math world they know. Programming teachers keep saying use variables that make sense, but variables with more than one letter do not make sense in the math and math is their only close framework at the moment. There is no previous framework for them to put this weird variable into. This idea is not a big deal to most kids but in typical teaching fashion we show it one minute and tell them to use it the next. There are also the little confusers like parentheses or square brackets and reserved words they may not know that they try to use for a variable name. (Trying to use “sum” was in my learning curve). So even line 1 is not as simple as it looks.

Now line 2. “Why do we need to do that?” “Because we need to define the variable before we use it.” “How come we do not have to define the ‘i’?” “Because it is the index variable in the ‘for’ loop and is self defining.” “Yah, right, whatever you say.” I can convince the kids of this one fairly easy.

Line 3 is part of the standard programming curriculum. A nice simple “for” loop with two variables, one of which is a list of numbers and not a number at all, and the other keeps changing. All with a colon on the end that does some magic. Simple syntax memorization. Now I just have to convince the kids this is the right programming doohickey to use. My students are in at least their second semester of programming so they know the syntax but they still have issues with strategy. Most can see the need to use a “for” loop. Since this is the first time they have used lists in “for” loops (Python thing) it does require learning a new capability of the “for“ loop.

Lines 4 and 5 are the money lines. These are the logic lines that seem to stump most of the kids. The “if” part is well understood. From there things can get a little fuzzy for them. Determining if an integer is even or odd or divisible by something by looking at the remainder seems totally foreign to the kids. Now that I think about it math curriculum has done some evolving that has eliminated the remainder from the curriculum. In pre-calculator years the remainder was used a lot. Long division and fractional answers have sort of gone the way of the slide rule and with it the use of remainders. Remainders were useful in finding factors and roots of polynomials but those techniques were replaced by the graphing calculator and algebraic computation software. The kids are just not used to using them. Now let’s throw in the new operator, mod (or modulus if you prefer), that the kids have no experience with. They have to remember the % sign means something other than percent. Then pile it on with a double equal Boolean operator which may also be a new symbol. That simple “if” statement the kids thought they knew has become a monster.

Line 5 makes absolutely no sense, at least if you have been taking math for the last umpteen years. There is no way that n can equal n plus some non-zero number. “Wait, what do you mean that is not an equal sign! Is too, just look at it!” An equal sign is an equal sign and the kids know an equal sign. That stupid equal sign is the bane of programming teachers. Personally I think this is one of the really big mistakes in programming languages. That equal sign is confusing and needs to be replaced by some kind of assignment symbol. I know there are languages that use a <= symbol which is a bit better.

The kids can get line 6 on their own (usually). They know there has to be an output somewhere. With Python’s indent format they sometimes get confused what it should be lined up with but they will get it on the second try if necessary.

So how easy is this little program? From the beginning programmers’ perspective it is anything but easy. Unfamiliar math, contradictory math caused by ambiguous symbols, new symbols and logic that is vague due to lack of background. No wonder the kids struggle and some people think programming is more difficult than brain surgery.