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
- 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
- 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"""
- self.values[-1] = 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"""
- pretend = []
- while(len(self.values)>len(pretend)):
- holder = random.choice(self.values)
- if (pretend.count(holder)>0):
- pass
- else:
- pretend.append(holder)
- self.values = pretend
- 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
- 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