A Beginner's Python Tutorial/for Loop
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:
- Code Example 1 - The for Loop
# Example 'for' loop # First, create a list to loop through: newList = [45, 'eat me', 90210, "The day has come, the walrus said, \ to speak of many things", -67] # create the loop: # Goes through newList, and seqentially puts each bit of information # into the variable value, and runs the loop for value in newList: print(value)
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 2 - A for loop example
# cheerleading program word = 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 + "!")
A couple of things you've just learnt:
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 all there is to the for loop.
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.
- Code Example 3 - A menu function
# 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. It is a good idea to comment your programs extensively. Remember that if you are going to be publishin gyour 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.
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 4 - Plain-english version of 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 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):
- Code Example 5 - Text Adventure Game
#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 introductary 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("") # Remember that a backslash continues # the code on the next line print "Light floods into the room as \ you open the door to your freedom."
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.)
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. 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 Example 6 - Creating an inspect function
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 Example 7 - The new game
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.