The Python Game Book

code games. learn Python.

User Tools

Site Tools


en:secret: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:secret:goblins:step008 [2013/07/08 12:12]
horst [compareValues function]
en:secret:goblins:step008 [2020/05/03 21:35] (current)
horst ↷ Links adapted because of a move operation
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|{{:slowgoblins008comparedemo.png?300|click me}}]]
 === output === === output ===
  
en/secret/goblins/step008.1373278323.txt.gz · Last modified: 2014/01/09 11:07 (external edit)