Monday 15th July

15/07/2012

Today, once again there are two options.

Scratch

The first is a little Scratch game I have created based on Frogger.

This is a really simple game, but it shows some of the basics you might need to know to make a game in Scratch.

First you need to create a background – using a colour for the “water” or “road” in the middle, and a different colour for the safe areas at the top and bottom. To do this, click on the “stage” icon and then choose to edit the background in the middle window.

Your next job is to create a “log”. I just painted a brown rectangle in the built-in editor, but you could use one of the Sprites that comes with Scratch, or make something different. The six “logs” will need a script each, something like this:

This will place the log at the edge of the screen and keep moving it across until it reaches the other size. At that point it is put back at the beginning. I’ll leave it up to you to work out how to get a block to start in the middle and keep looping around.

The “frog” will need a script like this:

This should look simple enough. If you get stuck, let me know and I will give you a bit of help.

Python

If you prefer to work on Python, you can return to the program we we’re looking at last week, if you wish. Alternatively, I have been working on a little program that works out the Caesar Ciphers we were looking two  weeks ago  in class.

It’s a gui program and the output looks like this:

I’m not going to give you all the code this time. You can download part of it here: code.py.

I have given you the code to set up the GUI – you’ll see something like this if you run it. What I have taken out is the function “submit()” which takes the input from the text box and encodes or decodes it.

I’d like you to have a go at writing this function. If you get stuck, you can ask me for a bit of help.

Helpful Hint

You’ll probably want to make a list or string with the letters of the alphabet. You can then use a method like “string.find(‘a’)” or “list.index(‘b’)” to give you the position of a certain letter in the string (or list). You can then add or subtract from this number to find the position of the new letter you’ll need.

 

Happy Hacking

antiloquax


Monday 9th July

08/07/2012

Turing

Today, I’d like you to do some research into the life of Alan Turing. Last week, in preparation for this, we had a look at the Google Doodle based on a Turing machine and explored some simple cryptography.

A good place to start is the wikipedia entry on Turing.
There is also some good information on the Bletchley Park website and the Alan Turing website.

Have a read and answer the questions on the sheet.

 

IRC

I thought you might like to be able to chat online to each other in this lesson. Have you used swiftirc before? Click the link, pick a nickname and put #9b2 in the “Channel” box. Simples.

 

 

Scratch / Bubble Sort

If you complete that, there are a couple of programming exercises you can work on.

The easiest is to learn about Bubble Sort and try out this Scratch version. This will show you one way that computers sort lists of numbers.

 

 

Python and Pygame

If you’d prefer to do some Python, here’s a file to download. It’s a very simple starting point for a game. You’ll also need this sound file to be saved in the same folder as the code.

If you look at the code, you’ll see how easy it is to use sound in Python and make keys do things (in this one, we use the arrow keys to move the ball). I’d like you to experiment with modifying this game – for instance you could add some different sounds and make some keys do other things (can you make a key play the sound, for instance).

If you are experimenting with this, you might find the Pygame homepage useful.

 

Programming Competition
The Raspberry Pi foundation has announced the first of their programming competitions. They especially like games. I wonder if any of you will enter?

happy hacking,

antiloquax.

 

 

 


The Killers (Example of good dialogue).

12/05/2012

The Killers  by Ernest Hemingway

The door of Henry’s lunchroom opened and two men came in. They sat down at the counter.

“What’s yours?” George asked them.

“I don’t know,” one of the men said. “What do you want to eat, Al?”

“I don’t know,” said Al. “I don’t know what I want to eat.”

Outside it was getting dark. The streetlight came on outside the window. The two men at the counter read the menu. From the other end of the counter Nick Adams watched them. He had been talking to George when they came in.

“I’ll have a roast pork tenderloin with apple sauce and mashed potatoes,” the first man said.

“It isn’t ready yet.”

“What the hell do you put it on the card for?”

“That’s the dinner,” George explained. “You can get that at six o’clock.”

George looked at the clock on the wall behind the counter.

“It’s five o’clock.”

“The clock says twenty minutes past five,” the second man said.

“It’s twenty minutes fast.”

“Oh, to hell with the clock,” the first man said. “What have you got to eat?”

“I can give you any kind of sandwiches,” George said. “You can have ham and eggs,bacon and eggs, liver and bacon, or a steak.”

“Give me chicken croquettes with green peas and cream sauce and mashed potatoes.”

“That’s the dinner.”

“Everything we want’s the dinner, eh? That’s the way you work it.”

“I can give you ham and eggs, bacon and eggs, liver—”

“I’ll take ham and eggs,” the man called Al said. He wore a derby hat and a black overcoat buttoned across the chest. His face was small and white and he had tight lips. He wore a silk muffler and gloves.

“Give me bacon and eggs,” said the other man. He was about the same size as Al. Their faces were different, but they were dressed like twins. Both wore overcoats too tight for them. They sat leaning forward, their elbows on the counter.

“Got anything to drink?” Al asked.

“Silver beer, bevo, ginger-ale,” George said.

 “I mean you got anything to drink?”

“Just those I said.”

“This is a hot town,” said the other. “What do they call it?”

“Summit.”

“Ever hear of it?” Al asked his friend.

“No,” said the friend.

“What do they do here nights?” Al asked.

“They eat the dinner,” his friend said. “They all come here and eat the big dinner.”


The Destructors

26/03/2012

Graham Greene’s classic story The Destructors.

Please read this story. Click on the link to open the pdf file.

We’ve looked at two stories in which the characters do something challenging in order to prove something to themselves (and perhaps others). In the next few lessons I want you to plan and write a story in which a young person does something unusual and difficult.

Make a start by thinking about characters and setting.


Two “Games”

18/03/2012

I am hoping that you are getting used to using IDLE to write, save and run programs. If you are enjoying these lessons, why not install Python on your computer at home? There are some instructions on the python website (just click on the icon to my left).

Alternatively, you might want to try a Linux environment. The easiest way to get started is RacyPy – it comes with Python and pygame ready-installed. If you need any help with this, just ask me. This is perfect if you are thinking of getting a Raspberry Pi and want to learn how to use Linux while you learn Python.

I thought we’d have a go at two simple games this week. The first is the classic “Guess the Number” thing.

Findin Python a bit hard? Why not try the Scratch version over on TeamPython!

Because I am so good to you, you can download the file here.

The start should look familiar to you. Some comment lines and then we import random so that we can generate a random number.

The print lines should also be no surprise to you. There are a couple of new things though: when we put “\t” into a string we are printing, Python adds a tab space. “\n” makes it give us a new line.

We then use the “randint” method (which we used last week if you had a go at the dice program). In a classic example of sensible variable-naming, this goes into “the_number”. We set guess to “True” so that we don’t get an error when we get to the while loop.

The while loop that follows will keep going until we guess the number (!= means not equal to). Every time we make a guess the computer tells us if we need to go lower or higher.

When we get it right, we exit the loop and get congratulated!

Have a go and see how it works. However this program is not perfect by any means. For instance, if we guess a number that’s over 100 or less that 1, the program just carries on telling us lower or higher. Do you think you could write some code that prints a sarcastic message if we make such a silly mistake?

And what if we enter something that isn’t a number? Try it and you’ll notice that the program crashes with an error. The problem is that we are trying to convert that answer into a number, so we get an error if the user types something silly. It’s a bit harder to stop this kind of error, but ask me in the lesson if you want to know how.

Anyway … have a play with this program and see if you can improve it at all.

The next program is a version of the same game, but this time, the computer has to guess our number.

I’ll let you type this one in :D.

It starts with some instructions. Then we import the random modules again (we’re going to make the computer’s guesses have some randomness to make it more fun). We set “ans” to the empty string(“”), “tries” to zero and make a guess that’s a random number between 1 and 100.

The variables: “high” and “low” are going to store the upper and lower limits of what the number could be, so they start off at 100 and 1.

We have a while loop that keeps going until the computer gets it right (and we enter a”y”). The next lines might look a bit odd. The reason for this is that the input command only likes to print strings, so we need to make a string that has the computer’s guess in it before we use “input”. We add one to tries to keep track of how many guesses the computer takes.

If the computer isn’t right, and we tell it to pick a lower number, it sets the new “high” to one less than the current guess. If it was too low, we make the new “low” one more than the guess. The computer’s next guess is a random number between “low” and “high”, which makes it look more like a guess!

Have a go and see if you can break this program! This time, if we enter something other than “h”, “l”, or “y”, we don’t get an error, but it does mess things up a bit! Can you work out how to fix it?

And another thing – the start is a bit … sudden. It would be nice to have a bit of time to think of a number. Do you think you could add one of the pauses that we used in the “Dragons’ Cave” program?

Here’s the fix for the bogus answers. Add these lines right after: ans = input(write):

if ans not in (“y”, “l”, “h”):
print(“Huh?”)
continue

Now, if we type something silly, the computer says “Huh?” and asks us about the same number again. This sort of thing is called “error trapping” and it’s important to do it if you don’t want your program to go wrong. In fact an important part of testing a program is to throw some silly stuff at it and see what happens. If it “breaks” then you need to do some error-trapping.

Here’s how I fixed the integer problem in the first guess program:

As you can see, there’s a command “try:” that we can use, which tests to see if a particular operation is going to give an error. This stops the program crashing!

happy coding

antiloquax


Back to Basics

10/03/2012

Objective: For students to become more comfortable using IDLE and writing simple programs.

Success Criteria

ALL: Will use the IDLE editor to save and run a simple program.

MOST: Will run at least two programs, with growing confidence in using Python.

SOME: Will make their own modifications to the programs we are working on.

Last week, some of us got a little confused when we were using Python’s IDLE tool, so here’s a quick refresher.

First, from the Start Menu, choose: All Programs > Python > IDLE.

This will open the Python Shell (or Interpreter). This is where we can enter Python commands and have them executed immediately. It’s useful for testing out individual bits of code, but we don’t use it to write programs.

We need to choose: File > New Window, and then we can write a program.

The first thing I’d like you to do is to “save as” and give the file a “.py” extension. This means that the editor will know we are writing a Python program. Our first program today is going to to be another “while” loop. But instead of the infinite one we did before, this one will only loop around the number of times we want.

This is a nice simple one and you can type it in yourself. Hopefully, you recognise the comment lines now (beginning with: “#”). Then we set the variable: “count” to 0 (zero). We need to do this or the “while” command in the next line will give an error (since we’d be asking it to check a variable that didn’t yet exist). The “while” statement will make the following lines run until “count” gets to 10. In the “while” loop, we have a simple “print()” command. The last line of the program adds one to count.

You can get the next program here. Once you have saved in in your documents, you can open it with IDLE.

We start by setting name to: “None”, so that the “while” loop will work. It will keep the program going until we press enter (as we don’t give it a name to work with). Try running the program. Hopefully most of it will make sense to you. Try changing the names and messages and let your friends have a go.

The next program we are going to look at includes a new idea: random numbers. We are going to use them to simulate the rolling of two dice.

You can get the program here. We have to “import” the module “random” so that we can use random numbers. Next we set variables to zero and use a while loop that will keep going until we get a double 6. The program gets its random numbers using two slightly different methods. The first, “random.randint()” gives us a random number between 1 and 6. On the other hand: “randrange()” gives us a random number between zero and 5 (this is because the computer starts counting at zero). That’s why we have to add 1 to get our dice throw. The rest is pretty easy.

Task: Try changing this program so that it does heads and tails instead of dice. It would be nice to have, say, three coins and to stop the program when we get 3 heads.

If you manage that, let’s try something else: a “for” loop. The “for” loop is very useful and it works a little differently to “while”. Here’s a simple program that prints out a multiplication table.

The “for” loop sets “i” to 1 and keeps adding one to it until it gets to 13. Enter this program and try modifying it to make it do something a little different (perhaps add a certain amount instead of multiply).

It doesn’t only work with numbers.

Try entering this and running it. Have a go at playing around and modifying it to do something else!

That’s all I’m going to put on the blog for today. By now you are getting used to writing simple programs. If you get finished, just ask me for something else to work on, or go over to Team Python, for some other blog posts.

antiloquax


This week’s programming lesson (5/3/12)

04/03/2012

Last week, many of you wrote your first ever Python programs.  I was impressed by how quickly most of you picked it up. I am going to try to make sure that there are plenty of ideas on here to keep you busy.

As you know, I made this blog especially for you and these lessons we have on a Monday. There is more stuff on Python, Linux and the Raspberry Pi over on Team Python.

Anyway … let’s start with something nice and simple.

I want you to get into good habits when you are writing programs. First, you should use comments. These make your code easier to understand and easier to fix, if something doesn’t work. You should also use some empty lines for the same reason. Another important thing is to use sensible variable names. For instance, I used “add”, “minus” etc for the results of those calculations – when you start writing longer programs you will save yourself a lot of hassle if you use variable names that mean something to you.

Snip: I’ve taken out the section on making a graphical game with pygame. I want to make some improvements, so I’ll repost it separately.

If you get that done, you might want to have a go at this. It’s quite simple, but fun. I won’t explain it much here – we’ll talk about it in class if you get this far.

Hopefully quite a lot of this is looking familiar. There are some new things in this, like random numbers and “sleep” which makes the game more fun!

happy coding

antiloquax