backlinks to this page:
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:
click reload on your browser if you see no source code here or visit Github.com
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.
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
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
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 objecthave 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.
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.
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.
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.
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~~