Omigosh: Happy New Year
January 22, 2011 1 Comment
Ximinez: Nobody expects the Spanish Inquisition! Our chief weapon is surprise…surprise and fear…fear and surprise…. our two weapons are fear and surprise…and ruthless efficiency…. Our three weapons are fear, surprise, and ruthless efficiency…and an almost fanatical devotion to the Pope…. Our four…no… amongst our weapons…. amongst our weaponry…are such elements as fear, surprise…. I’ll come in again. (exit and exeunt)
Well, I guess you didn’t expect that…
The end of our teaching year fell upon us somewhat abruptly last year, with the final tutorial on 25 October. We are now in a new year and have to make a start on some new tutorials. This tute is just to take stock of where we were up to, what we were doing and why we were doing it.
Let’s refresh our memory on what we’ve covered recently. This is what’s happened in the last few tutorials:
Ministry of Silly Objects: About objects, with some examples of methods and attributes. In particular, we used a file object which was created by the open() function. Methods are functions which are bundled with the object, while attributes are data which are bundled with it. In the tutorial, for example, the object fileObject had an attribute called name. The name attribute was accessible by putting a dot between object and attribute, thus: fileObject.name.(<- this last dot is a full stop, it’s not part of the Python and if you are observant you will see it is not italicised) Similarly fileObject had a method called close() which closed the file when you’re finished with it. That method was accessed by fileObject.close().
It turns out that everything in Python is an object. We saw that what we thought was data, for example a string, can have methods. This, for example, shows a string method (try it yourself):
"This will be turned into uppercase".upper()
In this tutorial we also built a little on the filing tutorial (see next) in that we saw that files on the hard disk are accessed through file objects that Python creates. The file objects allow you to do things like read from and write to the file.
Filing: We learnt a little about files. Files are used to store data on the computer (usually on the hard drive). Now that we know about files, we can store data so that we don’t have to keep typing it in every tutorial. In particular, we were working on a trivia game, which means there will be a heap of questions and possible answers in it. We will be using files to store these questions and answers between sessions. The storage of data into a file is also called “serialization” (the data is “serialized”) or persistence (the data is “persisted”). In truth, serialization is a way of conditioning data so that it can be persisted.
Trivial Lists: We learnt about lists in this tutorial, with the view to using a list to store a question and the answers to that question. If you remember, we stored each question in a separate list, with the wording of the question being entry 0, the correct answer being entry 1 in the list, and with a number of incorrect answers making up the other entries in the list. This approach had one problem that we identified – the person playing the game could always just choose the first answer and would always get it right (since we stored the right answer first).
Trivia Game (Part 2): In this tutorial we made use of the randint() function in the random package to randomise the location of the correct answer. This overcomes the problem we found in the previous tutorial from the way we stored questions. In this tutorial we also created a function called askQuestion() which took a question list as an argument. and asked the question, including randomising the position of the correct answer. This way, if we have a question, we can simply pass it off to the function to have it asked.