ThePythonGameBook

learn Python. Create Games

User Tools

Site Tools


en:python:goblins:step002

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
en:python:goblins:step002 [2011/12/11 17:08]
Horst JENS [playing with python's direct mode]
en:python:goblins:step002 [2017/09/03 16:21] (current)
Horst JENS
Line 1: Line 1:
-[[:en:python:step001| ← previous]] [[en:python:start| ↑ Overview]] [[:en:python:step003| → next ]] +===== 002 values ===== 
-====== Step 002 - thousand goblin dice duels ======+[[step001|←]] ​ | step 002 [[start|↑]] ​ ​|  ​[[step003|→]] ​ |
  
-If you tried **balancing** the values for the goblins Stinky and Grunty in [[en:​python:​step001|step 001]] you will feel the law of [[wp>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**. ​+=== changing variables ===
  
-The **for loop** **iterates** over 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).+During ​game of goblin dice duel, each hit on the head will decrease ​the victim'​s remaining hitpoints.
  
-===== playing with python'​s ​direct mode =====+Let'​s ​assume that Stinky swings his club and hit Grunty over the head so that Grunty looses one hitpoint ( or hitting him for 1 point of damage).
  
-This is a good time to play around with python's direct mode. If you start the editor **Idle** (shippedas 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 <​code>​python</​code>​. The result in both cases is that you are in python'​s **direct mode**, symbolized ​by the 3 leading "​greater than" signs: <​code>>>></​code>​ +In python, ​this is done by writing either:
- +
-As soon as you enter some commands and press the <​key>​ENTER</​key>​ python will try to interpret the command and print the result. This is handy to try out functions you do not know by heart, to access python'​s help system with the **help()** command or -if you are really bored- create some infinite loops like that one:+
 <code python> <code python>
-while True: +hitpointsGrunty = hitpointsGrunty - 1
-   print "​it'​s never boring with you"+
 </​code>​ </​code>​
- +or more common because less to type:
-==== list ==== +
-Let's use python'​s direct mode to learn about list's and the **range** function. A list in python is a comma seperated list of items, starting and ending with square brackets. If you want to put text strings in the list you need quotes:+
 <code python> <code python>
->>>​shoppinglist ​["​banana","​apple",​555,​ 3.13]+hitpointsGrunty -1
 </​code>​ </​code>​
-The interesting thing about list's is that each list item gets an number, starting with 0. You can access an list item by it's number: 
-<code python> 
->>>​shoppinglist[0] 
-'​banana'​ 
->>>​shoppinglist[2] 
-555 
-</​code>​ 
-More about list later or if you are interested here: http://​www.swaroopch.com/​notes/​Python_en:​Data_Structures 
  
-==== range ====+In both cases, the python interpreter will first calculate the right side of the equation (hitpointsGrunty - 1) and assign the result to the variable on the left side of the equation....hitpointsGrunty. The old value of the variable is overwritten.
  
-If you just want to have a list with 1000 items in it, all you need to type is+=== source code === 
-<code python> +//click reload on your browser if you see no source code here or visit [[https://​github.com/​horstjens/​ThePythonGameBook/​blob/​master/​python/​goblins/​slowgoblins002.py|Github.com]] // 
->>>​mylist ​range(1000) +<html><script src="​http://​gist-it.appspot.com/​github/​horstjens/​ThePythonGameBook/​blob/​master/​python/​goblins/​slowgoblins002.py?​footer=minimal"​></script></html>
->>>​mylist +
-</code>+
  
-You will get a list starting with "​[0,​1,​2,​3,​4,"​ and ending with "​997,​998,​999] 
  
-==== for loop ====+=== test === 
 +Tricky question: How many hitpoints has Grunty left now ?
  
-You can iterate over this list with a **for loop** (and break out of it using the **break** command): +Find out by either coding in your pyhton shell or online (just click on the graphic): \\ 
-Write this little program in the direct ​mode or in your python ​text editor:+[[http://​www.pythontutor.com/​visualize.html#​code=hitpointsStinky+%3D+22%0AhitpointsGrunty+%3D+43%0AhitpointsGrunty+-%3D+1&​mode=display&​cumulative=true&​heapPrimitives=true&​drawParentPointers=false&​textReferences=false&​showOnlyOutputs=false&​py=3&​curInstr=3|{{:​en:​python:goblins:​slowgoblins002.png|click on the graphic}}]]
  
-<code python> 
->>>​ for x in range(100): 
- print x 
  
-</code>+The answer is 42((If you don't laugh now, you should read [[wp>The_Hitchhiker%27s_Guide_to_the_Galaxy_(novel)|this book]])) 
 +=== inspecting variables ===
  
-It is now time to leave python'​s direct mode (if you work in a terminalpress <​key>​CTRL</​key>​ and <​key>​d</​key>​ and go back to your "​real" ​python ​text editor.+If you click the grey "​forward"​ button on the online example above you can see how the values of the variables change each time. IF you work with the python shellyou must tell python ​to show the value of a variable by typing the variable name:
  
 +Do this in the python shell:
 +<code python>
 +>>>​hitpointsGrunty
 +</​code>​
  
-====== flow chart ======+Later, you will learn other methods to inspect variables, like the print() function and how to work with debuggers.
  
-====== Code Discussion ====== +Generally, if you want to print the value of a variable, ​you code: 
- +<code python> 
-===== comments ===== +print(hitpointsGrunty) 
- +</code>
-====== ideas ====== +
- +
- +
-====== Source Code on Github ====== +
- +
-To start this example ​you need: +
- +
-^  file  ^  in folder ​ ^  download ​ ^  comment ​ ^ +
-|  [[https://​github.com/​horstjens/​ThePythonGameBook/​raw/​master/​python/​001_goblindice.py|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:// +
-<html> +
- <​script src="​http://​gist-it.appspot.com/​github/​horstjens/​ThePythonGameBook/​blob/​master/​python/​002_goblindice.py"​+
- </script></​html>​ +
- +
-^ [[:​en:​python:​step001| ← previous]] ^ [[en:​python:​start| ↑ Overview]] ^ [[:​en:​python:​step003| → next ]] ^ +
-====== Comment this Page ====== +
-~~DISQUS~~ +
-\\+
  
 +| [[step001|←]] ​ | step 002 [[start|↑]] ​ |  [[step003|→]] ​ |
/var/www/horst/thepythongamebook.com/data/attic/en/python/goblins/step002.1323619723.txt.gz · Last modified: 2014/01/09 11:07 (external edit)