#cashier.py
prices = {'milk': 1.00, 'wine': 2.50, 'apples': 0.6}
 
def sum_bill(purchase):
    """Calculate the total amount to pay"""
    total = 0
    for item, quantity in purchase:
        total += prices[item]*quantity
    return total
 
#Testing the code
if __name__=='__main__':
    my_purchase = [('milk', 2), ('wine', 1),  
                   ('apples', 1.2)]
    bill = sum_bill(my_purchase)
 
    print 'I owe %.2f Euros' % bill
#ecosystem.py
"""Module containing classes to simulate a simple ecosystem"""
 
class Organism(object):
    def __init__(self, position=(0, 0)):
        self.position = position
    def __str__(self):
        """string representation (family name)"""
        return "Living Organism"
    def update(self):
        """update the state of the organism"""
        pass
    def classification(self): 
        """return the systematic classification of the organism"""
        return self.__str__()
 
class Animal(Organism):
    def __init__(self, position=(0, 0)):
        Organism.__init__(self, position)
    def move(self):
        """change the postition"""
        pass
    def where(self):
        """get the current position"""
        return self.position
    def __str__(self):
        return "Animal"
    def classification(self): 
        # Hint: use __str__ method and print_classification of the
        # parent class
        pass
 
class Plant(Organism):
    max_size = 10
    def __init__(self, size=1, position=(0, 0), growth_factor=1.2):
        Organism.__init__(self, position)
        self.size = size
        self.growth_factor = growth_factor
    def update(self):
        self.grow()
        if self.size > self.max_size:
            self.reproduce(self.size/2.)
    def reproduce(self, child_size):
        """produce a sibling with size child_size"""
        pass
    def __str__(self):
        pass
    def grow(self):
        """grow the plant.
        the size is increased by a constant factor"""
        self.size *= self.growth_factor
 
 
if __name__ == "__main__":
    preanimal = Animal()
    print preanimal.classification()
 
code.txt · Last modified: 2009/08/30 23:45 by bartosz
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki