Guest User

Untitled

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