A Beginner's Python Tutorial/for Loop

Introduction
Well, in the first lesson about loops, I said I would put off teaching you the for loop, until we had reached lists. Well, here it is!

The 'for' Loop
Basically, the for loop does something for every value in a list. The way it is set out is a little confusing, but otherwise is very basic. Here is an example of it in code:

As you see, when the loop executes, it runs through all of the values in the list mentioned after 'in'. It then puts them into value, and executes through the loop, each time with value being worth something different. Let's see it a again, in a classic cheerleading call that we all know:
 * Code Example 1 - The for Loop

A couple of things you've just learnt:
 * Code Example 2 - A for loop example

As you see, strings (remember - strings are lines of text) are just lists with lots of characters. The program went through each of the letters (or values) in word, and it printed them onscreen.

Sometimes, you may only need to loop a certain amount of times (say 10). If you're coming from another programming language, you might be looking for the usual for loop in a format somewhat like this:
 * Code Example 3 - Looping through a range

Python doesn't have this type of loop. If you want to loop a certain number of times, simply use range.

A couple of things you must take note of:
 * The range starts with the starting number and ends up to the ending number. If you wanted to loop from 0 through 100, your range function will be range(0,101) as the function doesn't include the ending number.
 * The parameters for the range function are start (default of 0), end, and step (default is 1). A step is simply how much range will increment by. If you wanted to loop from 100 to 0, simply use a step of -1. In other words: range(100,0,-1).

Making a Menu Function
Now to the business end of the lesson. Lets start writing programs. So far we have learnt variables, lists, loops, and functions. That is pretty much all we need for quite a bit of programming. So let's set ourselves a task.

That wasn't very difficult, was it? the actual program only took up five lines - this is the wonder of how much we have learnt so far! All my comments take up sixteen lines - more than three times the program length. It is a good idea to comment your programs extensively. Remember that if you are going to be publishing your code open-source, there are going to be a lot of people checking out the code that you have written. We'll see the function we just wrote in our first example program.
 * Code Example 4 - A menu function

Our First 'Game'
What will our first example program be? How about a (very) simple text adventure game? Sounds like fun! It will only encompass one room of a house, and will be extremely simple. There will be five things, and a door. In one of the five things, is a key to the door. You need to find the key, then open the door. I will give a plain-english version first, then do it in python:


 * Code Example 5 - Plain-english version of code
 * 1) Plain-english version of our 'game'

Tell the computer about our menu function

Print a welcoming message, showing a description of the room. We will give the player six things to look at: plant, painting,\ vase, lampshade, shoe, and the door

Tell the computer that the door is locked Tell the computer where the key is

present a menu, telling you what things you can 'operate': It will give you the six options It will ask the question "what will you look at?"

if the user wanted to look at: plant: If the key is here, give the player the key otherwise, tell them it isn't here painting: same as above etc.   door: If the player has the key, let them open the door Otherwise, tell them to look harder

Give the player a well done message, for completing the game. From this, we can write a real program. Ready? Here it is (skip typing the comments):

Well, a very simple, but fun, game. Don't get daunted by the amount of code there, 53 of the lines are just the 'if' statements, which is the easiest thing to read there. (Once you comprehend all the indentation, you'll be able to make your own game, and you can make it as simple [or as complex] as you like.)
 * Code Example 6 - Text Adventure Game

Making the Game Better
The first question you should ask is "does this program work?". The answer here is yes. Then you should ask "does this program work well?" - not quite. The menu function is great - it reduces a lot of typing. The 'while' loop that we have, however, is a little messy - four levels of indents, for a simple program. We can do better!

Now, this will become much MUCH more straightforward when we introduce classes. But that will have to wait. Until then, let's make a function that reduces our mess. We will pass two things to it - the menu choice we made, and the location of the key. It will return one thing - whether or not the key has been found. Lets see it:

Now the main program can be a little simpler. Let's take it from the while loop, and change things around:
 * Code Example 7 - Creating an inspect function

Now the program becomes massively shorter - from a cumbersome 83 lines, to a very shapely 50 lines! Of course, you lose quite a bit of versatility - all the items in the room do the same thing. You automatically open the door when you find the key. The game becomes a little less interesting. It also becomes a little harder to change.
 * Code Example 8 - The new game