New Bamboo Web Development

Bamboo blog. Our thoughts on web technology.


Teaching programming to kids

about 1 year ago by pablo

New Bamboo's culture is about making ourselves and our clients happy, but we just don't stop at that. Recently, I joined an initiative to teach kids how to program. During 5 weeks, I joined Yali Sassoon and helped him hosting after-school programming lessons at Burlington Danes Academy School.

Once a week, we would get some brave volunteers (that's the kids!) who would let us introducing them to the wonderful world of computer programming. I had been for some time interested in the topic, and wanted to share my love for my trade with others. This was the perfect opportunity for me.

Yali did a great job organising the sessions, and we also received the kind assistance of the school, which hosted us. Thanks to both!

Preparation

For a programming language to use, Yali chose Python. I think it's a great choice, as:

  • It is a clear and explicit language
  • There are many resources on the web for it, for all levels
  • It is free and open source
  • It is relatively easy to install on Windows (compared to installing other environments!)

All that being said, there's a problem with Python nowadays: most resources out there are for Python 2, and many newcomers will be confused by the fact that Python 3 is the "latest" version. But I think this is a reasonable tradeoff if we can explain this to our pupils.

First contact

From the very first session, Yali wanted a very hands-on approach. We started by sitting the kids in front of computers and giving them a sheet describing very simple programs, taking them step-by-step through the very basics. Meanwhile, we were around helping them through the exercises, and answering their questions. There was no "lecture" part with kids looking at a whiteboard or anything similar.

Turns out we were in for a few surprises. How do you teach programming to somebody who has never done any? It is not apparent to us programmers, but our daily work involves a number of abstract concepts that most people have never heard of, and they are difficult to grasp at first. For example, have a look at the following program:

1 fahrenheit = raw_input("Enter a temperature in Fahrenheit degrees: ")
2 celsius = (float(fahrenheit) - 32) * 5 / 9
3 print "The temperature in Celsius is", celsius

First off: see that cast to float on the second line. Fortunately, this one is relatively simple to understand. Just don't dive too deep into details and explain that you need to be a bit explicit about numbers to avoid misunderstandings. This is something that newcomers can accept without much discussion. And this is not a contrived example: they are going to come across this issue very soon.

Now let's try to proceed from here. After introducing the "if" statement, we wanted the students to modify the program so it performs the conversion in either direction. We are hoping for a result similar this:

1 temp = raw_input("Enter a temperature: ")
2 unit = raw_input("What unit of temperature is that in?: ")
3 if unit == 'f':
4   result = (float(temp) - 32) * 5 / 9
5 else:
6   result = float(temp)*9/5+32
7 print "The converted temperature is", result

Suddenly, the program's complexity has exploded. Look at all the new concepts here. First the obvious ones:

  • There's if and else. We just explained them, but it's not enough with explaining. They need to crash against them in order to understand them.
  • Same goes for the double equals symbol '=='

Now, less obvious to programmers:

  • The indent. It's difficult at first to see the relation between statements and indent. This is not really a Python problem, I think. I bet the same problem would appear with alternative syntaxes, such as Java's braces or Ruby's begin...end.
  • We changed the name of the input value from fahrenheit to temp. However, a newcomer who is writing this program as a modification of the previous one will still call it fahrenheit. And that will make the code more difficult to understand. Same goes for result.
  • When prompted for input, they will enter things other than simply 'f' (or not 'f'). This will lead to the program taking the else branch when not intended. One to look out for: they will not print a space after "in?:" and instead will enter it on user input. As a result, unit will get a value of " f", which won't trigger the if condition.

Just that is enough material for a one-hour lesson.

Making it more interesting

That was our first attempt. I found that it was difficult and frustrating for the students. Also, they want to build something fun, like games, not silly programs on the command line. So we decided to do something different to spice it up a bit.

For our second session, we wanted to offer something more compelling, and this meant graphics. However, we also needed to make it as simple as humanly possible. This is the sample program we used, and it uses the Pygame library:

 1 import pygame
 2 
 3 pygame.init()
 4 pygame.key.set_repeat(1, 50)
 5 screen = pygame.display.set_mode([640, 640])
 6 circle_x = 320
 7 circle_y = 320
 8 font = pygame.font.Font(None, 50)
 9 message = font.render("", 1, (0,0,0))
10 keep_running = True
11 
12 while keep_running:
13   for event in pygame.event.get():
14     if event.type == pygame.QUIT:
15       keep_running = False
16     if event.type == pygame.KEYDOWN:
17       if event.key == pygame.K_LEFT:
18         circle_x -= 5
19       if event.key == pygame.K_RIGHT:
20         circle_x += 5
21       if event.key == pygame.K_UP:
22         circle_y -= 5
23       if event.key == pygame.K_DOWN:
24         circle_y += 5
25       key_name = pygame.key.name(event.key)
26       text = 'You pressed the key "' + key_name + '"'
27       message = font.render(text, 1, (0,0,0))
28   screen.fill([255, 255, 255])
29   pygame.draw.circle(screen, [0,0,0], [circle_x, circle_y], 20)
30   screen.blit(message, [10, 10])
31   pygame.display.flip()
32 
33 pygame.display.quit()

This program displays window and draws a circle on it. This can be moved with the arrow keys. For some extra appeal, it also displays a message informing of what key has been pressed. This is more fun to see than the previous examples. More importantly, there are a number of ways to modify this program to make new things, and even create some simple game. For example:

  • Don't let the circle dissappear offscreen: instead stop the circle from moving out of bounds, or make it appear on the opposite side.
  • Create some other object that moves on its own. First just in a straight line, dissappearing offscreen, then bouncing off the limits.
  • Change colors or shapes of the two objects. Pygame also allows them to load sprites.
  • Detect collisions between the two objects.
  • Add additional objects to make for other elements of the game. Maybe they are shots, like in a space shooter, or bricks, a la Breakout. Or whatever else they may choose.
  • Draw a starfield that moves creating an illusion of the other objects flying through space.

Challenges

This can go as far as imagination goes. However, before anything can be done, there are a number of challenges. The single most recurring pain point I found was that beginners don't realise that a program is executed in order, line after line, branching or repeating at control statements. This problem is difficult to see for a programmer, and difficult to explain to students.

As mentioned above, we didn't try to explain that in advance with the help or a whiteboard/projector, and I still wonder whether it would have been useful or not. Would they have paid attention and understood, or would have they spaced out at the sight of yet another teacher explaining boring stuff? One way or another, I had to sit down with several kids for 10 minutes each, leading them through each step of the program until they started to understand the flow. I didn't mind that at all, and learned new things with each new student; different ways in which it was difficult to understand, and ways to address those. However, this approach doesn't scale in a normal class!

And once that is out of the way, there's more:

  • Some physics is helpful. If you are going to program an object that bounces off walls, you need to understand concepts such as position and velocity. Younger kids really struggle at those, as they haven't been through physics yet.
  • Programs can start getting very large. Teaching object orientation, or just simple functions, is yet another level of abstraction that we couldn't reach within four lessons. Oh, and arrays (or lists, as they care called in Python) are yet another concept that cannot be taught until you are completely sure that they have fully understood loops.
  • Documentation. The program above doesn't show how to draw sprites, or how to detect that several keys have been pressed at once. For me, it's easy to look those up on the Internet, but not for beginners. How to look up documentation is a tremendously useful skill that needs to be taught sooner than later (we didn't get to that). Why, I can even think of adult, professional programmers who are not very good at it.
  • And I could keep writing...

Epilogue

I really enjoyed the experience, and would do it again. We, as novice teachers, were learning as much as the kids. And we were in for a few surprises, in the form of kids who surpassed our expectations and progressed as far as to create a playable game.

If I ever repeat this experience, I would like to try out new things. For one, I want to know if it's useful to explain things to the classroom using a proyector, instead of leaving them to their own devices from the start. Also, when using the graphical program, I wonder if I should skip the event loop initially and only add it in later, when more basic concepts have been grasped.

Oh, here's some footage of the kids talking about the experience: