ThePythonGameBook

learn Python. Create Games

User Tools

Site Tools


en:python:goblins:step008

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:step008 [2013/07/08 08:23]
Horst JENS [008]
en:python:goblins:step008 [2014/01/09 11:07] (current)
Line 25: Line 25:
 And a reminder of the ''​if''​ ''​elif''​ ''​else''​ structure as well. And something about local scope of variables. And return values. In short, let's practice a bit the things you learned so far. You best type this whole example in your python editor: And a reminder of the ''​if''​ ''​elif''​ ''​else''​ structure as well. And something about local scope of variables. And return values. In short, let's practice a bit the things you learned so far. You best type this whole example in your python editor:
  
- +=== source code === 
-<code python slowgoblins008compareDemo.py> +//click reload on your browser if you see no source ​code here or visit [[https://​github.com/​horstjens/​ThePythonGameBook/​blob/​master/​python/goblins/slowgoblins008compareDemo.py|Github.com]] // 
-def sign(a,​b):​ +<html><script src="http://gist-it.appspot.com/​github/​horstjens/​ThePythonGameBook/​blob/​master/​python/​goblins/​slowgoblins008compareDemo.py?footer=minimal"></​script></​html>​ 
-    """​compares a with b and returns a "<","​="​ or ">" sign"""​ +=== online ​===
-    if a b: +
-        return "<"​ +
-    elif a > b: +
-        return ">"​ +
-    else: +
-        return "=" +
- +
-def compareValues(hpA,​ attA, defA, hpB, attB, defB): +
-    """​returns a string with a table comparing the values of A and B"""​ +
-    text = "​\n ​         Stiny | vs. | Grunty " +
-    text+= "​\n ​---------------+-----+-----------"​ +
-    text+= "\n hitpoints: {0:3d} |  {1}  | {2:​3d}"​.format(hpA, sign(hpA, hpB), hpB ) +
-    text+= "\n attack: ​   {0:3d} |  {1}  | {2:​3d}"​.format(attA,​ sign(attA, attB), attB) +
-    text+= "\n defense: ​  ​{0:​3d} |  {1}  | {2:​3d}"​.format(defA,​ sign(defA,​defB),​ defB) +
-    text+= "\n" +
-    ​return text +
- +
- +
- +
-hitpointsStinky ​22 +
-attackStinky ​+
-defenseStinky ​+
-hitpointsGrunty ​43 +
-attackGrunty ​+
-defenseGrunty ​+
- +
-# python lines can be several physical lines long if inside brackets +
-print(compareValues(hitpointsStinky,​ attackStinky,​ defenseStinky,​ +
-                    hitpointsGrunty,​ attackGrunty,​ defenseGrunty)) +
-</​code>​ +
 Or you can try it out online (click on the graphic below): \\ Or you can try it out online (click on the graphic below): \\
-[[http://​www.pythontutor.com/​visualize.html#​code=%0Adef+sign(a,​b)%3A%0A++++%22%22%22compares+a+with+b+and+returns+a+%22%3C%22,​%22%3D%22+or+%22%3E%22+sign%22%22%22%0A++++if+a+%3C+b%3A%0A++++++++return+%22%3C%22%0A++++elif+a+%3E+b%3A%0A++++++++return+%22%3E%22%0A++++else%3A%0A++++++++return+%22%3D%22%0A%0Adef+compareValues(hpA,​+attA,​+defA,​+hpB,​+attB,​+defB)%3A%0A++++%22%22%22returns+a+string+with+a+table+comparing+the+values+of+A+and+B%22%22%22%0A++++text+%3D+%22%5Cn++++++++++Stiny+%7C+vs.+%7C+Grunty+%22%0A++++text%2B%3D+%22%5Cn+---------------%2B-----%2B-----------%22%0A++++text%2B%3D+%22%5Cn+hitpoints%3A+%7B0%3A3d%7D+%7C++%7B1%7D++%7C+%7B2%3A3d%7D%22.format(hpA,​+sign(hpA,​+hpB),​+hpB+)%0A++++text%2B%3D+%22%5Cn+attack%3A++++%7B0%3A3d%7D+%7C++%7B1%7D++%7C+%7B2%3A3d%7D%22.format(attA,​+sign(attA,​+attB),​+attB)%0A++++text%2B%3D+%22%5Cn+defense%3A+++%7B0%3A3d%7D+%7C++%7B1%7D++%7C+%7B2%3A3d%7D%22.format(defA,​+sign(defA,​defB),​+defB)%0A++++text%2B%3D+%22%5Cn%22%0A++++return+text%0A%0A%0A%0AhitpointsStinky+%3D+22%0AattackStinky+%3D+6%0AdefenseStinky+%3D+9%0AhitpointsGrunty+%3D+43%0AattackGrunty+%3D+5%0AdefenseGrunty+%3D+3%0A%0A%23+python+lines+can+be+several+physical+lines+long+if+inside+brackets%0Aprint(compareValues(hitpointsStinky,​+attackStinky,​+defenseStinky,​%0A++++++++++++++++++++hitpointsGrunty,​+attackGrunty,​+defenseGrunty))%0A&​mode=display&​cumulative=true&​heapPrimitives=true&​drawParentPointers=false&​textReferences=false&​showOnlyOutputs=false&​py=3&​curInstr=14|{{:​en:​python:​goblins:​slowgoblins008comparedemo.png|click me}}]] +[[http://​www.pythontutor.com/​visualize.html#​code=%0Adef+sign(a,​b)%3A%0A++++%22%22%22compares+a+with+b+and+returns+a+%22%3C%22,​%22%3D%22+or+%22%3E%22+sign%22%22%22%0A++++if+a+%3C+b%3A%0A++++++++return+%22%3C%22%0A++++elif+a+%3E+b%3A%0A++++++++return+%22%3E%22%0A++++else%3A%0A++++++++return+%22%3D%22%0A%0Adef+compareValues(hpA,​+attA,​+defA,​+hpB,​+attB,​+defB)%3A%0A++++%22%22%22returns+a+string+with+a+table+comparing+the+values+of+A+and+B%22%22%22%0A++++text+%3D+%22%5Cn++++++++++Stiny+%7C+vs.+%7C+Grunty+%22%0A++++text%2B%3D+%22%5Cn+---------------%2B-----%2B-----------%22%0A++++text%2B%3D+%22%5Cn+hitpoints%3A+%7B0%3A3d%7D+%7C++%7B1%7D++%7C+%7B2%3A3d%7D%22.format(hpA,​+sign(hpA,​+hpB),​+hpB+)%0A++++text%2B%3D+%22%5Cn+attack%3A++++%7B0%3A3d%7D+%7C++%7B1%7D++%7C+%7B2%3A3d%7D%22.format(attA,​+sign(attA,​+attB),​+attB)%0A++++text%2B%3D+%22%5Cn+defense%3A+++%7B0%3A3d%7D+%7C++%7B1%7D++%7C+%7B2%3A3d%7D%22.format(defA,​+sign(defA,​defB),​+defB)%0A++++text%2B%3D+%22%5Cn%22%0A++++return+text%0A%0A%0A%0AhitpointsStinky+%3D+22%0AattackStinky+%3D+6%0AdefenseStinky+%3D+9%0AhitpointsGrunty+%3D+43%0AattackGrunty+%3D+5%0AdefenseGrunty+%3D+3%0A%0A%23+python+lines+can+be+several+physical+lines+long+if+inside+brackets%0Aprint(compareValues(hitpointsStinky,​+attackStinky,​+defenseStinky,​%0A++++++++++++++++++++hitpointsGrunty,​+attackGrunty,​+defenseGrunty))%0A&​mode=display&​cumulative=true&​heapPrimitives=true&​drawParentPointers=false&​textReferences=false&​showOnlyOutputs=false&​py=3&​curInstr=14|{{:​en:​python:​goblins:​slowgoblins008comparedemo.png?300|click me}}]]
 === output === === output ===
  
Line 95: Line 63:
 If you wonder how to create this vertival line, the "​pipe"​ <​key>​|</​key>:​ Depending on your keyboard layout, you must press another key to get it (german keyboard layouts need to press <​key>​ALT GR</​key>​ and <​key><​nowiki><</​nowiki></​key>​. You may have a tool on your Computer named **Charmap** that helps you identify signs of your keyboard and remap them. In the worst case, use a <​key>​!</​key>​ instead of the pipe.</​WRAP> ​ If you wonder how to create this vertival line, the "​pipe"​ <​key>​|</​key>:​ Depending on your keyboard layout, you must press another key to get it (german keyboard layouts need to press <​key>​ALT GR</​key>​ and <​key><​nowiki><</​nowiki></​key>​. You may have a tool on your Computer named **Charmap** that helps you identify signs of your keyboard and remap them. In the worst case, use a <​key>​!</​key>​ instead of the pipe.</​WRAP> ​
  
-As you may have noticed i use no longer the ''​print()''​ function but instead i create and return a giant text string. A side effect is that i loose all those line endings that the ''​print()''​ function was taking care for me. So i have to code a **new line** sign, the **''​\n''​** everywhere in the code where i want a new line. (I love to do that at the start of each code line so that i can not forget it at the end of the code line). Inside a text string, something beginning with an **backslash** is often a [[wp>​Control_character]] or an [[wp>​Escape_sequence]].+As you may have noticed i use no longer the ''​print()''​ function but instead i create and return a giant text string. A side effect is that i loose all those line endings that the ''​print()''​ function was taking care for me. So i have to code a **new line** sign, the **''​\n''​** everywhere in the code where i want a new line. (I love to do that at the start of each code line so that i can not forget it at the end of the code line). Inside a text string, something beginning with an **backslash** is often a [[wp>​Control_character]] or an [[wp>​Escape_sequence]]. See the [[http://​docs.python.org/​3/​reference/​lexical_analysis.html#​string-and-bytes-literals|python documentation]] for a list of useful Escape sequences.
  
-The reason not to use ''​print()''​ instead of the text string lays in the future: I want to make this example work on webpages or in a [[wp>​Graphical_user_interface]] where ''​print()''​ does not work. Textstrings,​ on the other hand, will work and will be passed as arguments to some ultra-modern "make this visible with many colors"​ function. 
  
 +The reason not to use ''​print()''​ instead of the text string lays in the future: I want to make this example work on webpages or in a [[wp>​Graphical_user_interface]] where ''​print()''​ does not work. Textstrings,​ on the other hand, will work and will be passed as arguments to some ultra-modern "make this visible with many colors"​ function.
  
 === calling the funciton(s) === === calling the funciton(s) ===
/var/www/horst/thepythongamebook.com/data/attic/en/python/goblins/step008.1373264632.txt.gz · Last modified: 2014/01/09 11:07 (external edit)