I’ve recently become involved in a new computer programming club at my kids’ school. The club runs on Friday afternoons after school and is still very new so we’re still working through exactly what it will look like long term. These are my thoughts on the content from this first session. The point of this first lesson was to approach a programming problem where every child stood a reasonable chance of finishing in the allotted 90 minutes. Many of the children had never programmed before, so the program had to be kept deliberately small. Additionally, this was a chance to demonstrate how literal computers are about the instructions they’re given — there is no room for intuition on the part of the machine here, it does exactly what you ask of it.
The task: write a python program which picks a random number between zero and ten. Ask the user to guess the number the program has picked, with the program telling the user if they are high, low, or right.
We then brainstormed the things we’d need to know how to do to make this program work. We came up with:
- How do we get a random number?
- What is a variable?
- What are data types?
- What is an integer? Why does that matter?
- How do we get user input?
- How do we do comparisons? What is a conditional?
- What are the possible states for the game?
- What is an exception? Why did I get one? How do I read it?
With that done, we were ready to start programming. This was done with a series of steps that we walked through as a group — let’s all print hello work. Now let’s generate a random number and print it. Ok, cool, now let’s do input from a user. Now how do we compare that with the random number? Finally, how do we do a loop which keeps prompting until the user guesses the random number?
For each of these a code snippet was written on the whiteboard and explained. It was up to the students to put them together into a program which actually works.
Due to limitations in the school’s operating environment (no local python installation and repl.it not working due to firewalling) we used codeskulptor.org for this exercise. The code that the kids ended up with looks like this:
import random # Pick a random number number = random.randint(0, 10) # Now ask for guesses until the correct guess is made done = False while not done: guess = int(raw_input('What is your guess?')) print 'You guessed: %d' % guess if guess < number: print 'Higher!' elif guess > number: print 'Lower!' else: print 'Right!' done = True
The plan for next session (tomorrow, in the first week of term two) is to recap what we did at the end of last term and explore this program to make sure everyone understands how it works.