There is a professor at the university teaching the same Python course I am teaching here at the high school. If the kids sign up they can get dual credit. I have met him a couple of times and he knows Python really well. I on the other hand can spell Python. He is not using any text book so it makes it a little difficult to synchronize the courses in any way. As a result I like to see what he is teaching and what assignments he is giving his college students so I can use some of them in my course. The courses need to be somewhat similar to rate the dual-credit classification. I also want his assignments so I can try to do them. So far all are well within my capabilities as a beginning Pythonner but I hit one of his that was much more than just interesting. It is/was a pain in the rear. I got part of it working and decided the other part was not worth the effort. Here is the problem.
The goal of this exercise is to write a script that asks the user to enter an integer, and then displays the names of all of the positive integers up to that number.
Please enter an integer: 3
The challenge is to write a script that will work for “large” numbers.
I got up to the two digit part working and that was ugly enough. Figuring the logic to the problem took me quite a while, not because it was particularly difficult but more because there were several possible approaches. The university prof sent me his solution, which was much simpler that mine, but there was a step in his logic that was simply not obvious and, if not seen, can make the code really ugly. This sort of points out an important lesson in coding, getting the correct algorithm or logic is usually more important than being an expert in a language. The code solution for this problem was well within me and my students’ capabilities but that little error in how to solve the problem led to a major can of worms.
Typically when I hit a code solution that is a can of worms (Isn’t “can of worms” an official programming term? If it isn’t, it should be.) I suspect one of three things: 1. I screwed up and need to start over on the problem, or 2. the problem really is a can of worms or, 3. I do not know enough of a language to solve the program in a nice elegant manner. At my present knowledge level of Python I usually suspect 3 but in this case it was a 1.
When I hit a can of worms problem I sits and I stares (usually at the ceiling) in an attempt to see another way to solve the problem. In this case I had a solution; I knew the solution would work so I kind of got hooked into making that solution work. I did not stare at the ceiling long enough. It is a lesson I need to impart to my students, not necessarily the ceiling staring part but the think before you type part. They all want to read the problem (usually incorrectly) and start hammering on the keyboard immediately. I also need to point out if it is ugly, think more or read more. It all goes back to problem solving which is by far the hardest thing to teach to a 17 year old.
Today we are going to rip apart one of their assigned problems: Count the number of even digits in an integer. Since there are several ways of approaching this problem and considering my own “can of worms” issue it would be a good time to discuss programming away from the computer, or Ceiling Staring 101.