Categories
Programming Python Teaching Programming

PythonTurtle delivers!

A few days ago, a coworker asked me what tool he should use to teach another non-programming coworker some programming.PythonTurtle

I thought a little, and suggested PythonTurtle, and then also demonstrated the builtin turtle.

I thought nothing much of it, but a few days later, when I entered his office, the non-programming coworker was sitting there, trying to draw a star of david! She said he left her to do this, and that it was really cool, and then she asked me “how can I repeat commands instead of typing them again?”.

I showed her “for i in range(10)”, and then a function definition. She was really excited about those. I then helped her a little with the star of david, showed her some fractals, and gave her some new homework.
My conclusions from her reactions:

  1. PythonTurtle is especially suited for non-programmers. Because of the non-intimidating UI, she did not realize she was programming until we specifically told her she was. She thought this was some introductory stuff before she starts “real programming”. I believe this is very important, as while the classic turtle has the same capabilities, it would have been very different working from a “scary” interactive prompt. (The friendly turtle image is a definite plus in that regard! :)
  2. When teaching with PythonTurtle and using “for i in range(10)” and “def triangle():”, loops and functions were very intuitive. She was actually asking for such tools, and when I told her about them, she understood them almost immediately!
  3. Any programming teaching aid that makes the student actually excited about loops and functions should be respected :)
  4. Since the goal was to draw specific shapes, it was easy getting the “success high” associated with programming, and it kept her really excited. It was amazing to see her reaction when she finished writing the triangle function, not sure if she wrote it right, and then calling it, and seeing it draw a triangle correctly.
  5. Even with all the praise I write here, it should still be taken with a grain of salt. It’s possible that a lot of her enthusiasm was because of her personality, and less because of PythonTurtle itself.

Final verdict: next time I need to teach someone to program, I’m going to reach for PythonTurtle.

Categories
C Programming Teaching Programming

Starting from Scratch – Part 1

About a month ago, someone I met asked me if I could give him C programming lessons. To protect his privacy I won’t talk about him much, besides saying that he is a serious guy and he doesn’t know programming at all.

The first thing I did was give him a general explanation of what C does, the compilation process and so on. I then showed him a simple program, and showed him how to compile it, and how to run it.
I think the first thing beginners see when they start to program with C is all the “unnecessary voodoo”. For someone just starting to program, “#include <stdio.h>” doesn’t make much sense, and he shouldn’t be bothered with it. The same applies to “int main(void)”. So I just told him to ignore that for now, it would be made clear later on.

The next thing I did was give him a C book, and telling him what to read, and more importantly, what not to read. Teaching books suffer from a kind of “split personality” problem – they try to be both a tutorial and a reference. They also cover things fully the first time around. For example, consider variable types.
A first timer doesn’t need to know about anything besides int. He really doesn’t need to know about size limitations. He shouldn’t be bothered by these issues in the first few lessons.

As I see it, the order of things to learn is:

  • General information such as how to compile, etc…
  • Meaning of variables, and variable assignments.
  • if-else, and then do-while.

So we covered these subjects in the first two lessons, and he was able to write some simple programs, such as compute the average of two numbers, and so on.

We wrote a simple ‘guess the number’ game together, and I used this opportunity to explain if-else by writing conditions for cases such as “warm” or “cold”, “too low” and “too high”. Simple while loops were explained as well: “keep playing as long as the number was not guessed”. We wrote some more programs after that.

As homework, I gave him the following problem:

Write a program to compute an average of numbers.
It will read numbers from the user, until the user gives the number -1.
After it finished reading the numbers, it will print their sum, and their average.
If the average is in the range, print: 0-55: failed, 56-72: could be better, 73-85: ok, 85-95: good, 95-100: excellent.

I knew this will be problematic, and indeed it was. He wrote a program that asked for three grades, and averaged them. It went on asking again until the grades 100, 100, 100 were entered – he thought that was nicer than -1.
Well, a few days ago we sat together, and I understood the problem:

He knew how to write C code, but he didn’t know how to program!

I simplified the problem. I asked him to write (during the lesson) a program that reads numbers from the user until a zero is entered. It will then output how many ones were encountered. He couldn’t write that as well, and that’s no surprise – he had a big concept to understand.

So I told him, let’s play this together. I will tell you numbers, and when I finish, you will tell me how many ones were there. I gave him ten numbers and I noticed he was counting on his fingers!

Grasping this opportunity, I told him to explain exactly what he does. Then I asked him to give me instructions on how to count numbers. The instructions were “read a number onto your left hand”, and “if the number on your left hand is one, set the number on your right hand to be the number on your right hand plus one”. It took some effort for him to understand this, but he did. I then asked him to do the same, but now summing the all of the numbers with the right hand.

I then showed him how I translate these instructions into C. I asked him for the definition of an average, and then, looking at the code on the screen, realization dawned on him. He was amazed – he managed to calculate the average of an unknown number of numbers!

It was amazing for me as well – it’s not often that you get to see someone understand a big concept such as programming.

After the averaging program I asked him to write a counter program – given a number, print all the numbers from zero up to this number. When he got stuck, I asked to turn off the screen, and again give me instructions on how to do the task. That he was able to do. Translating these instructions to C wasn’t that hard, and some 30 minutes later, he had a working program. Success!

Categories
Programming Programming Philosophy Teaching Programming

Zen Programming – 2

I thought about this a long time ago with Erez:

To fully grasp structured control flow – you must first learn to program without it.

Saw it happen, with myself and with others. Only after writing some hand-written conditionals and loops in assembly, do you really learn what do they mean, and how to make the code do what you want it do.

Categories
C Programming Programming Philosophy Python Teaching Programming

"Fnord" or "The evil empire cheerfuly striked back at the merry-colored pretty princess"

One of my favorite exercises for young programmers is the following (widely known program):

Write a program that will read lists of words from the following files:

  • verbs.txt – contains verb, the others are respectively –
  • nouns.txt,
  • adverbs.txt,
  • and adjectives.txt.

After reading the words, the program will construct sentences of the form:

The <adj> <noun> <adv> <verb> the <adj> <adj> <noun>.

You could of course allow for different sentence structures, or more types of words (prepositions). This site made me remember that. This exercise is really handy, and it also allows for great variations of difficulty (at least in C). If you tell the programmer that he or she must support variable number of words, or just a maximum, or how to define the files… the options are endless. The interesting thing about this exercise, is that in C, it is much more effort to write then in Python (with all the built-in modules). This makes the exercise even more effective: After letting the new programmer painfully implement it in C, I let the guy (or girl) implement it in Python. Really fun to watch. Especially after he or she discovers the meaning of ‘one-liner’ :)