A Beginner_s Python Tutorial-for Loop
A Beginner_s Python Tutorial-for Loop
1Introduction
2The 'for' Loop
3Making a Menu Function
4Our First 'Game'
5Making the Game Better
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!
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? ")
Python doesn't have this type of loop. If you want to loop a certain number of times, simply use
range().
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).
return input(question) - 1
# print ") " + entry prints a bracket, and then the entry name
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.
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 6 - Text Adventure Game
return int(input(question)) - 1
#The key is in the vase (or entry number 2 in the list above):
keylocation = 2
loop = 1
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 plant.")
print("")
keyfound = 1
else:
print("You found nothing in the 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. 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.)
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 8 - 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.