ThePythonGameBook

learn Python. Create Games

User Tools

Site Tools


en:python:goblins:step003

003 for loop

step 003

for loop

Grunty will not stay idle when Stinky is beating him, so it is now Stinky's turn to loose a hitpoint:

hitpointsStinky -= 1

Stinky has now 21 hitpoints left. And it is Grunty's turn again. And then Stinky's. And then Grunty's. And than again Stinkys. For how long ?

As this version of Gobling Dice Duel is rather dull and predictible, you can say that if Stinky (the less robust goblin) starts the game with 22 hitpionts and looses one hitpoint each round, then the game will end after 22 rounds when all of Stinkys hitpoints are gone.

Instead of alternately writing code like:

hitpointsStinky -=1
hitpointsGrunty -=1
hitpointsStinky -=1
hitpointsGrunty -=1
# ...and so on...

note the Hash sign ? In python, everything right of the hash sign is a comment. Comments are only important for us humans, the computer will mostly ignore them.

I can program the computer to subtract one hitpint from each goblin for 22 rounds using a for loop:

source code

click reload on your browser if you see no source code here or visit Github.com

code discussion

There a several new things in this example, and i will explain all of them. Important things first: Do you notice the colon (:) after line 3 ?

In python, lines after an ending colon are indented, usually by 4 spaces1). In this example, that means that both indented lines are repeated (“looped”) 22 times.

range

For, in and range are python keywords, while gameround is a new variable, the so called loop variable. The range(22) command ( try it out in the python schell! ) creates an generator2) who generates the integers of 0 to 21. Try it out in the python shell:

# do this in the python shell
>>> range(22)
range(0, 22)

So why the python shell writes range(0,22) ? The answer is written in the documentation of the range command. In the python shell, you can type

#do htis in the python shell
>>>help("range")

to display the documentation of the range command. It is possibly more convenient to simply browser at the python website and read it there. The exact url is: http://docs.python.org/3/library/stdtypes.html#range

Navigating the python website is not so easy. While there is an online search function, i found the range documentation by clicking: python homepage –> documenation –> 3.x documentation –> libary reference –> built-in functions –> range –> ranges

documentation on range

Whatever display you prefer, the documentation has this to say about range:

>>> help("range")
Help on class range in module builtins:

class range(object)
 |  range(stop) -> range object
 |  range(start, stop[, step]) -> range object
 |  
 |  Returns a virtual sequence of numbers from start to stop by step.
 |  
 ...

What does that tell us ? First, there are several ways to code a range command, all of them a valid:

range(22) # (stop value) all integers from 0 until (including) 21  
range(10,22) # (start value, stop value) all integers from 10 until 21
range(10,22,2) # (start value, stop value, step value) all even integers from 10 until 21

parameters

The numbers in round brackets after the range command are called parameters or arguments. As you can see, the range command accepts one argument, two arguments or three arguments. The square brackets around step in the documentation

  range(start, stop[, step]) -> range object

have the meaning that step (including it's leading comma) is optional. You don't have to write it. (If you do not give a step arguments, the range() functions give step the default value of 1). In short, the command range(22) creates an range object with all integers from 0 to (including) 21, but not with 22, because 22 is the stop value.

Python always start counting from 0, not from 1 as we humans do. You will see this later when slicing lists and working with data structures.

for loop

Now back to the for loop. the line

for gameround in range(22):

means that the indented lines below the colon will repeated (looped) 22 times. During the first loop, the variable gameround has the value of 0, because 0 is the first number that the range object generates. In the second loop, the variable gameround has the value of 1, in the next round the value of the variable gameround is 2 and so on.

It is a good idea to run this example with an python debugger or simply view it online (click on the graphic below):
Click on the graphic

loop variable

A for loop always need a loop variable, in this case the gameround variable. Each time the python interpreter runs the loop, the loop variable get another value ( in this case, starting with 0 and ending with 21). Please see the online example above or use an python debugger for visualisation.

iterating over lists with a for loop

In python there exist lists (more on that later) and other structures where a for loop can iterate over. An iterable can be things like all the characters in a word ( string ) , all numbers in a list, all physical lines in a text file etc. If you have no such iterable object ready, the range command will create one on the fly for you. More on lists and iterating in later chapters.

using documenation on for loops

As you managed to read the documentation of the range function, hopefully the documentation about the for loop will make a bit sense now: http://docs.python.org/3/reference/compound_stmts.html#the-for-statement ~~CLEARFLOAT~~

step 003
1)
or one tab. i prefer spaces
2)
in python2.x, it would generate a list
/var/www/horst/thepythongamebook.com/data/pages/en/python/goblins/step003.txt · Last modified: 2014/01/09 11:07 (external edit)