Back to Basics

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.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: