Tuesday, January 26, 2016

#PythonTuesday #Coding --- Dictionaries --- "Yoji defeated your visiting_cook!"


Just a simple dictionary based on the food! Imagine there is a battle in your kitchen...


  1.        #create dictionaries for the weapons (such as tomato,cheese,garlic...)
  2.  
  3. tomato = {
  4.     "name": "tomato",
  5.     "color": "red",
  6.     "health": 120,
  7.     "speed": 35
  8. }
  9.  
  10. cheese = {
  11.     "name": "cheese",
  12.     "color": "blue",
  13.     "health": 50,
  14.     "speed": 25
  15. }
  16.  
  17. garlic = {
  18.     "name": "garlic",
  19.     "color": "white",
  20.     "health": 80,
  21.     "speed": 70
  22. }
  23.  
  24. #create a dictionary for the enemy
  25. enemy = {
  26.     "name": "visiting_cook",
  27.     "health": 80,
  28.     "speed": 40
  29. }
  30.  
  31. #create a dictionary for our player.
  32. player = {
  33.     "name":None,
  34.     "weapon": None,
  35.     "health": None
  36. }
  37.  
  38. #assign the value to player by adding them to the dictionary.
  39. #player["weapon"] = "garlic"
  40. #player["health"] = garlic["health"]
  41. #player["name"] = "Yoji"
  42.  
  43. #assign different value...
  44. #player["weapon"] = "tomato"
  45. #player["health"] = tomato["health"]
  46. #player["name"] = "Yoji"
  47.  
  48. player["weapon"] = "cheese"
  49. player["health"] = cheese["health"]
  50. player["name"] = "Yoji"
  51.  
  52.  
  53. pname = player["name"]
  54. ename = enemy["name"]
  55.  
  56.  
  57. #define the function
  58. def kitchen_battle(pname,ename):
  59.     if player["health"] > enemy["health"]:
  60.         print "%s defeated your %s!" % (pname,ename)
  61.     elif player["health"] == enemy["health"]:
  62.         print "WOW! %s and your %s are strong opponents!" % (pname,ename)
  63.     else:
  64.         print "%s lost to your %s!" % (pname,ename)
  65.  
  66.  
  67. # call the function
  68. kitchen_battle(pname,ename)

  69. # end of the code
The final output:

Yoji lost to your visiting_cook!

If the player chose different weapon, then the result of your "kitchen battle" based on the comparison between two health points would be different as well...i.e. if we go with ''garlic", the final output would be:

WOW! Yoji and your visiting_cook are strong opponents!

if we chose "tomato", the result would be:
Yoji defeated your visiting_cook!

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Back to CodeCombat. Screenshot of Level "Lowly Kithmen":




Goals:
  • Your hero must survive
  • Defeat the ogres(2/2)
  • Collect the gems
  • Bonus: no code problems
Code:
enemy1 = self.findNearestEnemy()
self.attack(enemy1)
self.attack(enemy1)
enemy2 = self.findNearestEnemy()
self.attack(enemy2)
self.attack(enemy2)
self.moveRight(2)
self.moveDown()
self.moveLeft()


No comments:

Post a Comment