Python at key stage 3

a simple programI recently had the pleasure of introducing some year 8 and year 9 pupils to Python. They had previously dabbled in Scratch, but the idea of writing formal text-based programs was new to them, so we took it slow and steady.

The first lesson was mostly taken up with getting to know Python – we started by typing commands directly into the shell, and then they learned how to open up a new window, type a simple program into it and run the program. By the end of the first lesson they could type up print statements and simple maths statements into a program listing, save it correctly and run it.

Lesson 2 introduced the idea of variables and input. I reminded students at the beginning of the session how to start up a new window and create a program rather than type into the shell, and how to save their program with .py on the end to conserve the colour coding. They were also starting to discover the pleasures of debugging, and learning to look carefully at their code to spot errors. They helped each other out with this, and I showed them how to check not only the area of code that Python highlighted as a concern, but to check just before it as well, as often the error was missing speech marks or closing brackets.

Lesson 3 was where the real fun began, as we looked at if statements. Combining these with input meant that we could create a question and answer program, and combining that with a variable called score gave them the option of creating their very own quiz. Reactions varied, as some struggled to get their program running and grew frustrated while others started to really fly, creating inventive listings that checked for a user name and password before allowing access to the quiz. Generally, they seemed to enjoy the sessions, and my favourite reaction was that of frustration expressed as the code refused to work, followed by that surge of satisfaction and achievement as the error was spotted and fixed.

Our work developed by way of writing programs to solve problems, and for this I used my flash resources. This had the advantage of offering different levels of challenge and support, so that those who were able could work at a higher level while I could help those who were really struggling. By the end of the sessions all students understood a little more about how programs are made and they had learned that little details like capitals and spelling really matter. Most were starting to see how their program worked and how to fix errors and develop the functionality, while a small number really enjoyed the unit and were starting to come up with their own ideas and solutions.

This was only a very brief introductory look at programming, but it was enjoyable and worthwhile as a taster of computing rather than ICT, especially for those going into options, and encouraged students to think carefully and increase the accuracy of their written work. I saw expressions of satisfaction from both ends of the ability level, as those who struggled finally got something working while those who experimented showed off what they had managed to work out for themselves. Students whose first reaction as “This is stupid” moved over to “Oh, I see what the problem is”, as they learnt that the computer would do exactly what was it was told to do, and discovered for themselves the harsh realities of Garbage In Garbage Out. Even if they never see another Python statement, I believe they’ve taken away the understanding of how carefully you need to express yourself in order to be properly understood and how accuracy leads to better results.

As a result of my experiences I’ve developed a workbook and some flash resources, which you can find on my website here.

 

A Flash resource for Python

As I introduce Key Stage 3 groups to Python, I’m finding that some of them really “get it” and have started trying to develop and experiment with code, while others really struggle to make any sense of it at all.

In order to help them work more independently at all levels, to free me up to give more targeted help where needed, I wanted a resource I could use that would offer support at all levels.

I had an idea for an interactive Flash resource, to offer different levels of challenge, with support available if needed. I keep looking at Flash, telling myself that it’s not worth going into, and then reconsidering, so it was an ideal opportunity to see if I could make good use of it after all.

developing a resource in flash

Developing a resource in Flash

I came up with a simple idea for a coding project – working out the costs of something with a fixed element and a sliding element – that was within their mathematical understanding as well as programming experience but complex enough to require some thinking. Once the basic idea was there, I found ways to develop it, from a simple changing of the fixed and sliding costs to adding a discount which would require an if statement, and started to build my resource.

This was my first time creating an interactive flash resource for a specific purpose, but having tried out various tutorials I had at least a vague idea of how it worked, and as I progressed I found that all that theory started to make sense and I got a much clearer idea of how to proceed.

The end result was a resource that posed a problem, provided listings for students to copy if they needed, and then provided three levels of challenge, each of which could be tackled independently, again with code to copy if needed.

I tried it out with students of various abilities, all of whom are very new to Python, and all completed the basic program, the majority tried at least one challenge and the bravest went straight to the highest level of challenge.

Easy challenge

Easy challenge

While I wouldn’t recommend copying code indefinitely, students at the early end of learning to program definitely benefit from having something to work from, as checking capitals and spelling can be enough of a challenge to start with! They are starting to look more carefully at what they do, and to see how to adapt their code, and some are starting to show the buzz of excitement at getting a program running that shows they are getting a real sense of achievement at coding rather than just being an end user of software.

I’ve since adapted my resource to a second project, and while the first took a couple of evenings to complete the second one took only a couple of hours, and felt much easier to do, proving that Flash is a viable tool for creating interactive resources.

Both resources are available on my website.

 

Teaching game

teach logo

working title for game

I have an irrepressible habit of turning everything I can into a game, so when searching round for a game idea I could create for myself, my job seemed an obvious inspiration. One game I’ve enjoyed playing is Diner Dash, and I fancied the idea of creating a similar sort of game about balancing the needs of a class of students and reaching the goal of good exam marks.

It also seems to be the ideal type of game for object oriented programming, with classes of students extended into sub-classes of different types of student, all of whom have slightly different needs. One-to-one teaching is straightforward, but the wider the variety of students in the class the tougher it will be to achieve success. The possibilities are endless!

Having mapped out the original idea, I need to start with a single student and work out the balances between the different variables to achieve reasonable gameplay.

I decided to stick to Python for now, although other options are Flash, Greenfoot or Scratch. Each of these offer easier access to graphics, but if we are to be using Python in school I need to have as much experience in it as possible. Flash also has the drawback of not being available on devices using iOS, which suggests to me that its days are limited, and Scratch, while great for first experiments, feels too self-contained to have wider use, while Python and Java/Greenfoot are more widespread and will run on a wider variety of machines and situations.

Next issue was the choice of editor. I started writing my code in Geany, as this is more compatible with pygame and other GUI modules, but then remembered that for the earliest versions it’s easier to test as I go along and for that I need IDLE, which allows me to type in commands directly. Having written out a first example of my code in Geany, I ran it in IDLE, tested some of it, and then amended the code and tried it again. Here I hit the first problem: I suspect that IDLE and Geany handle the indenting slightly differently, and so the code written in the two different editors threw spacing/tabbing errors. Having forgotten my earlier learning and not kept a log or changed file version, it took a bit of fiddling before I was off again, but this time I kept a log open to keep track of progress.

Adding in complexity to make the variables more interdependent proved successful on one level, but I need to know exactly what effect each variable is having, so at this point I’m moving to a spreadsheet so that I can explore the impact of different variables and weightings on the overall game more easily. I also need to draw up a proper flowchart so I can see what is happening.

I’m beginning to realise that this game will take lots of skill in lots of different areas: a spreadsheet model to explore the effects of the variables, maths skills to play around with the weightings, an understanding of game mechanics to achieve a playable game, graphics design to create the graphics needed, writing skills to write game instructions and even music skills to create music for it. And that’s before I even get to the coding skills!

At this point also I’m beginning to understand more of what my students will need to do when writing up their coursework, and appreciating the need to keep careful records of design ideas and progress. And thinking of the fun I’ll have playing with the settings to make sure that I don’t hit problems with transferring back to Geany as my editor!

 

codeword success

codeword gui version 1

first idea for a GUI

It only took a screenshot of my project in action to put myself back on the right path, and a very pleasant afternoon was spent adding the last piece of functionality to it – as a reminder, it’s a program that allows me to enter the settings for a codeword grid, which is a puzzle that looks like a crossword but with number standing for letters. The user tries out a letter in place of a number and eventually solves the whole puzzle, with each number representing a different letter. My program does not create or provide puzzles, but instead you can use it as a tool to help solve commercially available puzzles, to save scrubbing out incorrect guesses.

I had got it to the point where it would display the grid in the console and accept changes via direct entry in the console, so what was left to do was to provide a list of what letters had been used already and to not allow the same letter to be used for two different numbers.

Writing code for the list proved to be straightforward – rather than delving into different data types and debating which was best, I opted for a simple list, with the first entry at index 0 set to “*” and the rest of the entries set to ” ” (space). Adding a letter for a specific number then involved setting the list at that index to the letter, and resetting involved setting the entry back to a space.

I could then call these list functions as part of the setting and resetting a letter, and check whether a letter had been used already before allowing it to be entered into the grid.

My code at the moment has very little help for the user (hence part of my problems yesterday!) and it has very little error checking for input, but neither of these is a real issue as the intention is to put a GUI on the front of the code, to handle display and entries.

And here I hit the next issues: issue one is how to create a GUI in python and issue two is which editor to use to create it.

The book I’ve been working from suggests pythoncard, but I have not been able to install this successfully. I’ve managed to establish that Tkinter is already available as part of the python standard library, but it carries a warning that it will not run with the basic python IDLE, as that does not handle events properly, so that then leaves me looking around for an editor that will do the job.

I enjoy working with the IDLE, as it’s incredibly straightforward, and I object to using more time figuring out how to use an editor than I spend actually coding. I’ve got Eclipse that I’ve used for Java, and I’m currently looking into using that with a pydev plugin, but I’m starting to get frustrated by all these different options, which all seem designed to provide high levels of support with very complex projects and make life very complicated for the casual user who just wants to create a simple project.

The other IDE that I’m trying out is Geany, which I recognise as the software that originally came on my raspberry pi, and so I have used already for python. I’ve got it running with my codeword software, but when it gets to the end of the program, which goes as far as creating the grid, the program then seems to terminate without allowing me to enter further commands via the console. If I can put a GUI onto the program, then the console won’t be needed anyway, but it’s just another frustrating step.

I think with fondness of the days when I learnt programming, when we used C++ with an IDE that allowed us to design a GUI and attach code to it really easily. Even the Java editor we used back in those days seemed far more straightforward, even though I never did really get the hang of the different containers that you could use for java layouts.

Surely these things should be getting easier, not more complex?

Incidentally I’m now running a log of my project, with notes for each version change on what I’m attempting to do. Each feature is being developed piece by piece, with constant resaves as different versions – all versions ending in 4X are the basic version, while 5X added in the list and 6X used the list to check whether the letter was available before using it. I’ve also included screenshots of the project in action and a first sketch of what the interface might look like.

Hopefully if I end up leaving the project again for an extended period (for example while I build up my GUI skills) this log will make it much easier next time to pick it up and catch up with progress. I can’t say I understand every line of the code I’ve written or could duplicate it easily, but at least I know enough to know what it does and how to use it, and which functions are available.

 

Picking up the pieces

codeword throwing an errorIt’s been a while – a hectic six weeks of term, until finally we’ve reached half term and I have time to try to pick up the pieces of my technical developments and see if I can make progress with them.

I left a few projects hovering: there’s the virtual pet that I started in Scratch and app inventor, the Simple Simon game that I made in Scratch and started to convert to app inventor, the general learning I was doing with Python and the codeword project that I started in Python.

It was the codeword project that’s been my ambition for longest, and the project that I remember as the furthest developed, so that’s the one I’ve opted for first. And there I hit my first problem.

Just in trying to pick up the pieces of this project, I’ve learned several things: that it’s really hard to pick up a complex project after so long, that without any real structure it’s difficult to pick it apart and get an overview, and that with no log of changes it’s hard to even find which version to use! I’ve got versions of the file from codeword to codeword6, plus codeword4a and codeword4b. I’ve gone back to codeword4b, as that contained a note saying it was the latest – but of course there’s nothing to say I didn’t progress further and just not remove the note. I remember the project being workable via command line codes, but now it doesn’t seem to respond to even the simplest of commands.

This blog is probably the place I’ve documented my project the most, so I guess I’ll have to read back and see if it’s any help. In the meantime, I really need to pick the code apart and understand it thoroughly, and produce some sort of map or algorithm so that I know how it all ties together – although algorithms/flowcharts become rather complicated when they’re event driven rather than procedural!

In the meantime I’ve learnt that even the most straightforward of projects needs to be thoroughly documented and planned before coding, and that actively writing and testing code is just a small part of any software project. Managing your code so that it can be easily understood and followed, and leaving some sort of explanation to progress, and building in error checking are also incredibly important.

I’ve also proved the point that software needs concentrated effort to get to grips with, and picking it up only for a short while can be counterproductive, as it can take so long to catch up with where you were that you never actually have time to make proper progress. On the other hand, maybe  little and often isn’t too much of a problem – but little and seldom definitely is a no-no!

Raspberry Pi time!

Although I have had the pi out and working once before, it’s been a while and there’s a new distribution out for the software, so it was like starting all over again: the first thing I needed to do was to download and unzip the new distribution (Raspbian wheezy) from the pi website.  This needs to be copied onto an SD card before you start.  You have to use a disk imager to set up the SD card, it can’t just be copied over, but I’d already downloaded a free disk imager ready.

pi with case

pi with case

The operating system for pi is a linux distribution, with a graphical interface very like Windows available. It also comes preloaded with several pieces of software such as Python and Scratch, making it very fast to get up and working for programming purposes.  It took about 20 minutes altogether to download the software and copy it onto the SD card ready for use, and then I could start plugging things in.

The pi comes with two USB sockets which are usually taken up with keyboard and mouse, and as the operating system is installed onto an SD card which needs recreating for a new setup, this time I was also trying out a powered USB hub, so that I can save any work I do onto a separate memory stick rather than on the SD card.  The Pi itself isn’t powerful enough to run a hub, so it has to be one with its own power supply.

I hit a major problem when connecting the screen – as I explained in an earlier post, I need to use an adapter to connect my screen to the pi, and it had a rather troublesome connection, but eventually we managed to get it working.  So I had the pi up and running, with powered hub running keyboard, mouse and memory stick, and with a cable draped across the room to the router to give an internet connection as well.

I was disappointed to note that this new distribution seems to have a lot less than the previous one in the way of preloaded software, but the essentials are there – web browser, python 2 (with pygame) and python 3 (without pygame), plus Scratch.  Squeeze is also listed, but didn’t seem to load up.

There is also a folder of python games, taken from the book Invent With Python, which I copied onto the memory stick to bring back to my main machine and look at.

The browser seemed to work with no problems at all, and it was as easy as plugging in the cable and typing in a web address to get online with the Midori browser, so that was the basics up and running, although I couldn’t see how to access the memory stick directly from applications such as Scratch – my husband said something about mounting the drive, but I felt copying over is enough for now, until I get to the point of using the pi properly and having files worth saving directly onto the external drive.

I feel now that maybe I’ve reached the point where I need to start looking carefully for instructions, either via the MagPi magazine or some of these books that have sprung up about the pi, in order to take it any further.  Scratch and Python are fine with no issues for me, but the operating system is a little unfamiliar for anything outside the very basic and I’m not confident enough to go poking around, downloading stuff and installing it without advice.

I do believe the pi is a wonderful gadget, but as yet I don’t think I’ve identified its full purpose – as a really cheap machine available to encourage kids to play with programming, it’s great, but apart from its price it doesn’t offer that much different from a desktop machine. I understand you can use it for input and output devices, rather like the arduino, but that again is something that would need research and setting up, although it does seem to me that one major advantage for the pi apart from price is the physical size of it and that seems to be begging to be embedded in some sort of system.

I think it’s time to go off and do some research into what people are using the pi for, and what it’s capable of!

PS the case is great for protecting the pi as it sits on  my desk – I found it on my husband’s desk (around his pi in fact! shh!)

Python codeword project

Another project I’ve been thinking of (for many, many years, actually, since I first came across the puzzle type) is a program to help solve codeword puzzles.  These are the ones that look like crosswords, except that each square has a number and each number stands for a letter of the alphabet, so you have to crack the code to solve the puzzle.  I love these puzzles but don’t like trying a letter out, changing my mind and having to scrub them back out of the book, so my idea is to create something that will replicate the grid on screen and allow you to type in a number and letter and have the program automatically replace for you, with a reset button for each number if you change your mind.

I think I’ve even made a half-hearted attempt to get going before, but never really got very far, but having seen the listings for python games using the console, including Reversi and tic-tac-toe (or othello and naughts and crosses if you prefer) I’ve realised that it’s worth making a start in console mode to get the basic functionality sorted, with a better interface as a future development.

I’ve had a frustrating time at some points in this project, but I’ve also learned a lot. There were two main learning foci – data structures and classes.  With data structures I think I hit problems because I was thinking too much about two dimensional arrays to store the grid, but in Python I have a list  of lists – the grid looks like [[codeword, codeword, codeword],[codeword, codeword, codeword]] where each of the inner lists represents one line in the grid and each object in that list is an object of a new class I’ve defined.

Python offers a useful way to iterate through lists: you simply call

for <element> in <list>:

and then you can run through them, for example:

for e in self.square:
    for f in e:
        if f.number==num:
            f.setLetter(let)

This cycles through all the elements in the main list, all the elements within that element, and if the number matches it sets the chosen letter.

The codeword object contains three attributes: it has a number, a letter and a boolean called fixed, which enables me to lock a letter/number combination when I’m sure it’s right (or if it’s one of those given as part of the original puzzle).

codeword console in action, showing grid drawn and redrawn

codeword console in action

So far I can display the grid in the console, change, lock, unlock or reset letters and redraw the grid to display any changes.  The next stage will be to provide a key, which shows which letters have been assigned and which are still available to use.

Then I need to tie the engine to an interface, as the console display makes it hard to see the words properly and it’s tiresome to have to type commands directly into the console rather than clicking on buttons.

The idea of writing classes and functions to deal with different aspects of the project is to make it modular, so that I can replace my current codeword object with a similar object that will respond to the same calls but display things differently, so that it’s easier to replace/modify small areas of code rather than having to write the whole lot again, but in Python you can define more than one class in the same file, and indeed have your main program code there as well, whereas in Java it’s usual to have one class per file and import them all.

I haven’t mastered the art of modules yet to include code from other files that I have created, but then I haven’t really needed to, as my entire project is only 134 lines long, including comments and whitespace.

There’s still plenty of room for improvement: as well as the interface upgrade, it’s also possible to provide an option to save and restore games, and the method of inputting the grid needs to be streamlined – at the moment I have to type the numbers in for each line separated by a space, with 0 for a blank.  A check is made that I’ve entered the right number of values for the line, but not that they’re correct, and any mistake means restarting.

Still, I’ve made a working prototype that’s proven the possibility, and had fun developing my thinking skills in the process.  I’m also starting to remember some of what I learned about interfaces to objects, encapsulation and various other topics.  I had an issue with Python’s typing at one point as it doesn’t need its variable types declared, and I was getting muddled between ints and strings, but it just means it’s down to me to watch what I’m doing rather than relying on the language to enforce type or flag up problems.

I’m constantly reminded, though, that it helps to understand what’s going on and what the principles are, and so the importance of understanding theory as well as having practical experience, and how each feeds into the other constantly.

I’ve provided the code below, if anyone’s interested in looking – it’s very much a Work in Progress, and probably very clumsy and muddled in places, so be warned!

codeword puzzle assistant