LESSON 7
The 'for' loop, and some programs of your own
Well, in the first lesson about loops, I said I would put off teaching you the 'for' loop, until we had reached lists. Now, I could have taught you lists before I taught you loops, but I guess that is something for the 2nd revision

.
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:
Code:
#A 'for' loop:
list = [45, 'eat me', 90210, 'The day has come, the walrus said, \
to speak of many things', -67]
#create a loop:
#Goes through [u]list[/u], and seqentially puts each bit of information
#into the variable [u]value[/u], and runs the loop
for value in list:
print value
As you see, when the loop executes, it runs through all of the values in the list 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:
#cheerleading program
word = raw_input("Who do you go for? ")
for letter in word:
call = "Gimme a " + letter + "!"
print call
print letter + "!"
print "What does that spell?"
print word + "!"
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. And that is the 'for' loop.
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.
But before that, we need to figure out some pieces of code that we will use a lot. What I'm thinking, is a menu. So, using our knowledge of lists, functions and input, let's write a function that acts as a menu. Here it is, have a GOOD look at it (remember - you don't have to type in the comments - the computer doesn't see them anyway.
Code:
#THE MENU FUNCTION
#The program asks for a string with all the menu options in it,
#and a text string asking a question.
#make sure every menu entry is unique.
def menu(list, question):
for entry in list:
print 1 + list.index(entry),
print ") " + entry
return input(question) - 1
#def menu(list, question): is telling the function to
#ask for two bits of information:
#A list of all the menu entries,
#and the question it will ask when all the options have been printed
#for entry in list: is pretty much saying;
#'for every entry in the list, do the following:'
#print list.index(entry) + 1 uses the .index() function to find
#where in the list the entry is in. print function then prints it
#it adds 1 to make the numbers more intelligable.
#print ") " + entry prints a bracket, and then the entry name
#after the for loop is finished, input(question) - 1 asks the question,
#and returns the value to the main program (minus 1, to turn it back to
#the number the computer will understand).
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. Not that daunting after all. We'll see it used in our first example program.
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:
#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: pot 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:
pot plant:
If the key is here, give the player the key
otherwise, tell them it isn't here
painting:
same as above
<snip>
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):
Code:
#TEXT ADVENTURE GAME
#the menu function:
def menu(list, question):
for entry in list:
print 1 + list.index(entry),
print ") " + entry
return input(question) - 1
#Give the computer some basic information about the room:
items = ["pot plant","painting","vase","lampshade","shoe","door"]
#The key is in the vase (or entry number 2 in the list above):
keylocation = 2
#You haven't found the key:
keyfound = 0
loop = 1
#Give some introductory text:
print "Last night you went to sleep in the comfort of your own home."
print "Now, you find yourself locked in a room. You don't know how"
print "you got there, or what time it is. In the room you can see"
print len(items), "things:"
for x in items:
print x
print ""
print "The door is locked. Could there be a key somewhere?"
#Get your menu working, and the program running until you find the key:
while loop == 1:
choice = menu(items,"What do you want to inspect? ")
if choice == 0:
if choice == keylocation:
print "You found a small key in the pot plant."
print ""
keyfound = 1
else:
print "You found nothing in the pot plant."
print ""
elif choice == 1:
if choice == keylocation:
print "You found a small key behind the painting."
print ""
keyfound = 1
else:
print "You found nothing behind the painting."
print ""
elif choice == 2:
if choice == keylocation:
print "You found a small key in the vase."
print ""
keyfound = 1
else:
print "You found nothing in the vase."
print ""
elif choice == 3:
if choice == keylocation:
print "You found a small key in the lampshade."
print ""
keyfound = 1
else:
print "You found nothing in the lampshade."
print ""
elif choice == 4:
if choice == keylocation:
print "You found a small key in the shoe."
print ""
keyfound = 1
else:
print "You found nothing in the shoe."
print ""
elif choice == 5:
if keyfound == 1:
loop = 0
print "You put in the key, turn it, and hear a click"
print ""
else:
print "The door is locked, you need to find a key."
print ""
print "Light floods into the room as you open the door to your freedom."
Well, a very simple, but fun, game. Soon you'll make your own, and you can make it as simple (or as complex) as you like. I'll post quite a few, later.
The fist 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 lines 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. It 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:
Code:
def inspect(choice,location):
if choice == location:
print ""
print "You found a key!"
print ""
return 1
else:
print ""
print "Nothing of interest here."
print ""
return 0
Now the main program can be a little simpler. Let's take it from the while loop, and change things around:
Code:
while loop == 1:
keyfound = inspect(menu(items,"What do you want to inspect? "),keylocation)
if keyfound == 1:
print "You put the key in the lock of the door, and turn it. It opens!"
loop = 0
print "Light floods into the room, as you open the door to your freedom."
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.
Now I said you would write some programs now. Here is your chance! Your task, if you chose to accept it, is to post a better text adventure game. When you are finished, post them here (in a .txt file - the forums don't let you post python files). You can use any of the code I have given you here. Remember to check back on previous lessons we have done - they are priceless tools. Do a search for some simple text adventure games - if you find some nice, fun (and clean) text adventure games, post a link here - some of us have never ventured that far back into gaming history, and need education.
You can ask me absolutely any question you like of me - I am willing to help, even if that means looking an answer - or finding someone who knows the answer

. I'll be writing some very documented text adventure games myself - I'll post them here, and maybe teach you a thing or two.
Thanks to all,
Gingerbread Man.