Tuesday, November 10, 2009

A program for twelve year old programmers

When I was little, I would have found this program interesting:

import turtle

def fib(n):

    if n<2:



I still do!

The program is in python.

Play with it. Change the numbers and see what happens. Make it be in colour. Make each branch a different colour.

Once you have done this, you will understand something fun.

Nick Rolfe has translated it into logo, so you can run it in a web browser. Unfortunately it has to be Firefox rather than Internet Explorer.
You can still play with it and modify it in Firefox, just like you can in python.

The program is in python. I can not tell you how to install python, because it is different on every computer. But some computers already have it. And it is very easy to install on the ones that don't. The best way to run the program in python is to use IDLE, which is part of python.

If you run it in IDLE, the turtle's window will not disappear when the turtle has finished. If you'd like to run it any other way, Steven's comment below will tell you how to stop the window disappearing.

My python is version 2.6.4, but I imagine it will work in any later python as well, and possibly some earlier ones too.

I am sure that I will be corrected if I am wrong.

Footnote for anyone coming via reddit

Forgive me.

The article before this has been the top hit on the programming reddit all day, with the title "Why has this got so much harder to do? Why kids don't program any more."
The associated discussion handed me my ass on a plate, so I wrote this new program to show how easy it was for a modern child to explore recursion, which wouldn't have been at all easy in ZX Basic thirty years ago.

I was assuming without thinking about it that people who read the first would read the second and vice versa, which was a bit dim of me.


  1. Here is a quick port to a version of Logo that runs in a web browser.

    (I haven't run the Python version yet, so I hope mine is equivalent).

  2. Not quite. I think logo stop causes an immediate exit? Python's pass is just 'do nothing', but it drops through to the forward(-30). The result should be a tree.

    Fix that and major bravo. I'll modify the post to say "If you can't make it work on your own computer here's a web version". What's your full name so I can credit you?

  3. Here is the fixed version. That makes much more sense.

    My name is Nick Rolfe. Thanks!

  4. Sweet, thanks Nick. There's now a lovely pleasing similarity to the coloured version just below, as well!

  5. Put this line: raw_input("Press Enter to quit.")

    At the end of the program so we can admire our turtle's hard work :)

  6. Steven, thank you for pointing that out. Serves me right for always writing python in IDLE!

    I hope you'll forgive me for not following your suggestion, since I think it's more fun to play with these sort of things at the REPL, but I've added a note, and hopefully anyone who runs it stand-alone will find your comment to make the window stay open.

  7. The turtle module seems to be much slower than Logo on the Apple //c, and that doesn't sit right with me.

  8. Was suprised this just works in windows, thought I'd need to install turtle first somehow.

  9. Just and FYI, my 9 y.o. daughter is reading your blog now. LOL

  10. Thank you for letting me know.

    Hello! You've made me very happy :-). I hope you like my tree!