Advertisement
Vermiculus

Untitled

Apr 1st, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Use docstrings.
  2. # http://docs.python.org/tutorial/controlflow.html#intermezzo-coding-style
  3. # http://www.python.org/dev/peps/pep-0257/
  4. ##random needed for mix method. Needs to be external to the class
  5. import random
  6.  
  7. class Bowl:
  8. ##with a little help from: http://stackoverflow.com/questions/227459/ascii-value-of-a-character-in-python
  9. ##as well as: http://www.rafekettler.com/magicmethods.html
  10.  
  11.  
  12. def __init__(self, values = []):
  13. """sorry to change your code, Sean but this allows Bowl to be treated as a list"""
  14. self.values = values
  15.  
  16. def __len__(self):
  17. """returns length of Bowl"""
  18. return len (self.values)
  19.  
  20. def delitem(self, key):
  21. """deletes the item at key in self"""
  22. del self.values[key]
  23.  
  24. def top(self):
  25. """returns the last element in self.values. (top of the stack)"""
  26. return self.values[len(self.values)-1]
  27.  
  28. def put(self, ingredient):
  29. """appends value to self"""
  30. self.values.append(ingredient)
  31.  
  32. def fold(self, ingredient):
  33. """sets value of the top of the stack to ingredient"""
  34. self.values[-1] = ingredient
  35.  
  36.  
  37. def add(self, ingredient):
  38. """ ##This adds the value of ingredient to the value of the ingredient
  39. ##on top of the bowl and sets that sum as the new top of the stack,
  40. ##erasing what was there.
  41. """
  42. holder = self.top()
  43. holder = holder + ingredient
  44. self.values[-1] = holder
  45.  
  46.  
  47. def remove(self, ingredient):
  48. """##This subtracts the value of ingredient from the value of the ingredient
  49. ##on top of the bowl and sets that difference as the new top of the stack,
  50. ##erasing what was there.
  51. """
  52. holder = self.top()
  53. holder = holder - ingredient
  54. self.values[-1] = holder
  55.  
  56. def combine(self, ingredient):
  57. """This multiplies the value of ingredient from the value of the ingredient
  58. on top of the bowl and sets that product as the new top of the stack, erasing what was there.
  59. """
  60. holder = self.top()
  61. holder = holder * ingredient
  62. self.values[-1] = holder
  63.  
  64.  
  65. def divide(self, ingredient):
  66. """This divides the value of ingredient from the value of the ingredient
  67. on top of the bowl and sets that quotient as the new top of the stack, erasing what was there.
  68. """
  69. holder = self.top()
  70. holder = holder / ingredient
  71. self.values[-1] = holder
  72.  
  73. def mix(self):
  74. """This randomises the order of the ingredients"""
  75. pretend = []
  76. while(len(self.values)>len(pretend)):
  77. holder = random.choice(self.values)
  78. if (pretend.count(holder)>0):
  79. pass
  80. else:
  81. pretend.append(holder)
  82. self.values = pretend
  83.  
  84. def liquify(self):
  85. """This turns all the ingredients in the bowl into a liquid, i.e. a Unicode characters for output purposes."""
  86. better = []
  87. for x in range (len(self.values)):
  88. better.append(chr(self.values[x]))
  89. self.values = better
  90.  
  91. ##
  92. def __str__(self):
  93. """to string method, so one can print Bowl when needed """
  94. return str(self.values)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement