Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##random needed for mix method. Needs to be external to the class
- import random
- class Bowl:
- ##with a little help from: http://stackoverflow.com/questions/227459/ascii-value-of-a-character-in-python
- ##as well as: http://www.rafekettler.com/magicmethods.html
- ##Constructor!
- ##sorry to change your code, Sean but this allows Bowl to be treated as a list
- def __init__(self, values = None):
- self.values = []
- ##Length
- ##returns length of Bowl
- def __len__(self):
- return len (self.values)
- ##deletes the item at key in self
- def delitem(self, key):
- del self.values[key]
- ##returns the last element in self.values. (top of the stack)
- def top(self):
- return self.values[len(self.values)-1]
- ##appends value to self
- def put(self, ingredient):
- self.values.append(ingredient)
- ##sets value of the top of the stack to ingredient
- def fold(self, ingredient):
- self.values[-1] = ingredient
- ##This adds the value of ingredient to the value of the ingredient
- ##on top of the bowl and sets that sum as the new top of the stack,
- ##erasing what was there.
- def add(self, ingredient):
- holder = self.top()
- holder = holder + ingredient
- self.values[-1] = holder
- ##This subtracts the value of ingredient from the value of the ingredient
- ##on top of the bowl and sets that difference as the new top of the stack,
- ##erasing what was there.
- def remove(self, ingredient):
- holder = self.top()
- holder = holder - ingredient
- self.values[-1] = holder
- ##This multiplies the value of ingredient from the value of the ingredient
- ##on top of the bowl and sets that product as the new top of the stack,
- ##erasing what was there.
- def combine(self, ingredient):
- holder = self.top()
- holder = holder * ingredient
- self.values[-1] = holder
- ##This divides the value of ingredient from the value of the ingredient
- ##on top of the bowl and sets that quotient as the new top of the stack,
- ##erasing what was there.
- def divide(self, ingredient):
- holder = self.top()
- holder = holder / ingredient
- self.values[-1] = holder
- ##This randomises the order of the ingredients
- def mix(self):
- pretend = []
- while(len(self.values)>len(pretend)):
- holder = random.choice(self.values)
- if (pretend.count(holder)>0):
- True
- else:
- pretend.append(holder)
- self.values = pretend
- ## This turns all the ingredients in the
- ## bowl into a liquid, i.e. a Unicode characters for output purposes.
- def liquify(self):
- better = []
- for x in range (len(self.values)):
- better.append(chr(self.values[x]))
- self.values = better
- ##to string method, so one can print Bowl when needed
- def __str__(self):
- return str(self.values)
Advertisement
Add Comment
Please, Sign In to add comment