Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Use docstrings.
- # http://docs.python.org/tutorial/controlflow.html#intermezzo-coding-style
- # http://www.python.org/dev/peps/pep-0257/
- ##random needed for mix method. Needs to be external to the class
- 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
- def __init__(self, values = []):
- """Constructor"""
- #sorry to change your code, Sean but this allows Bowl to be treated as a list
- #-Actually, this is what I had in mind,
- # although I don't see where we'd need to have the copy constructor ('values = []')
- # but it can't hurt
- self.values = values
- def __len__(self):
- """returns length of Bowl"""
- return len (self.values)
- def delitem(self, key):
- """deletes the item at key in self"""
- del self.values[key]
- def top(self):
- """returns the last element in self.values. (top of the stack)"""
- return self.values[-1]
- def put(self, ingredient):
- """appends value to self"""
- self.values.append(ingredient)
- def fold(self, ingredient):
- """sets value of the top of the stack to ingredient"""
- ingredient = self.values.pop()
- return ingredient
- def add(self, 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.
- """
- holder = self.top()
- holder = holder + ingredient
- self.values[-1] = holder
- def remove(self, ingredient):
- """##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.
- """
- holder = self.top()
- holder = holder - ingredient
- self.values[-1] = holder
- def combine(self, ingredient):
- """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.
- """
- holder = self.top()
- holder = holder * ingredient
- self.values[-1] = holder
- def divide(self, ingredient):
- """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.
- """
- holder = self.top()
- holder = holder / ingredient
- self.values[-1] = holder
- def mix(self):
- """This randomises the order of the ingredients"""
- import random
- random.shuffle(self.values)
- def liquify(self):
- """This turns all the ingredients in the bowl into a liquid, i.e. a Unicode characters for output purposes."""
- # better = []
- # for x in range (len(self.values)):
- # better.append(chr(self.values[x]))
- # self.values = better
- self.values = map(chr, self.values)
- def stir (self, minutes):
- dad = []
- butts = self.values.pop()
- self.values.insert(len(self.values)-minutes, butts)
- ## self.values.insert(minutes - 1, self.values.pop())
- def __str__(self):
- """to string method, so one can print Bowl when needed """
- return str(self.values)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement