ThePythonGameBook

learn Python. Create Games

User Tools

Site Tools


en:python:goblins:step002

This is an old revision of the document!


Step 002 - thousand goblin dice duels

If you tried balancing the values for the goblins Stinky and Grunty in step 001 you will feel the law of Law_of_large_numbers: If Stinky wins 5 times in a row over Grunty, that does not proof that Stinky's values are superior over Grunty. Maybe Stinky has a streak of luck or Grunty's random numbers are specially unlucky. Only if you test a game (several) thousands times you can start to make qualified statements.

Well, pushing thousand times the “run program” button on my text editor is not my idea of fun. (Maybe later if the program becomes better). Good thing that there is a python function to repeat a given task a many times: the for loop.

The for loop iterates over a given list. If you give the for loop a list with thousand items to iterate over, it will do the same task thousand times. But where to get a list with thousand items ? Lucky for you, python provides also a function to generate lists: the range function (and the xrange function).

playing with python's direct mode

This is a good time to play around with python's direct mode. If you start the editor Idle (shipped, as you know, together with python), you are by default in the direct mode. If you -like me- dislike idle (inner values aside, the thing is simply ugly) you can open a terminal and enter

python

. The result in both cases is that you are in python's direct mode, symbolized by the 3 leading “greater than” signs:

>>>>

flow chart

Code Discussion

comments

ideas

Source Code on Github

To start this example you need:

file in folder download comment
002_goblindice.py python Download the whole Archive with all files from Github:
https://github.com/horstjens/ThePythonGameBook/archives/master
version for python2.x


python 2.x

View/Edit/Download the file directly in Github: https://github.com/horstjens/ThePythonGameBook/blob/master/python/002_goblindice.py

click reload in your browser if you see no code here:

Comment this Page

~~DISQUS~~

/var/www/horst/thepythongamebook.com/data/attic/en/python/goblins/step002.1323618826.txt.gz · Last modified: 2014/01/09 11:07 (external edit)