An exercise from fundamentals of python
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/8374300/fundamentals-of-python-chapter-8-project-3">Fundamentals of Python Chapter 8 project 3</a>  </p>
</blockquote>



<p>Hi I am a newbie programmer who just started to learn about python.
I have recently posted this same question before and I have solved it but my answer is not exactly what the question is asking. </p>

<p>I need help on why I need to implement a new method even though I could do the other way.</p>

<p>thanks</p>

<p>Question:</p>

<blockquote>
  <p>The <code>__str__</code> method of the <code>Bank</code> class returns a string containing the
  accounts in random order. Design and implement a change that causes
  the accounts to be placed in the string by order of name. 
  <em>[this is the part where I don't understand]</em>
  <strong><em>(Hint: You will also have to define a new method in the SavingsAccount class.)</em></strong></p>
</blockquote>

<pre><code>class Bank(object):

    def __init__(self):
        self._accounts = {}
    def __str__(self):
        """Return the string rep of the entire bank."""
        pTemp =[]
        for i in xrange(len(SavingsAccount.temp)-1):
            if self._accounts.get(SavingsAccount.temp[i]).getName() &gt;= self._accounts.get(SavingsAccount.temp[i+1]).getName():
                temp = SavingsAccount.temp[i]
                SavingsAccount.temp[i] = SavingsAccount.temp[i+1]
                SavingsAccount.temp[i+1] = temp
        for i in SavingsAccount.temp:
            pTemp.append(self._accounts[i])
        return '\n'.join(map(str, pTemp))

    def add(self, account): 
        """Inserts an account using its PIN as a key."""
        self._accounts[account.getPin()] = account
    def remove(self, pin):
        return self._accounts.pop(pin, None)
    def get(self, pin):
        return self._accounts.get(pin, None)
    def computeInterest(self):
        """Computes interest for each account and 
        returns the total."""
        total = 0.0
        for account in self._accounts.values():
            total += account.computeInterest()
        return total

class SavingsAccount(object):
    """This class represents a Savings account
    with the owner's name, PIN, and balance."""
    RATE = 0.02
    temp = []
    def __init__(self, name, pin, balance = 0.0):
        self._name = name
        self._pin = pin
        self._balance = balance
        SavingsAccount.temp.append(self)
    def __str__(self):
        result =  'Name:    ' + self._name + '\n' 
        result += 'PIN:     ' + self._pin + '\n' 
        result += 'Balance: ' + str(self._balance)
        return result
    def getBalance(self):
        return self._balance
    def getName(self):
        return self._name
    def getPin(self):
        return self._pin
    def deposit(self, amount):
        """Deposits the given amount and returns the
        new balance."""
        self._balance += amount
        return self._balance
    def withdraw(self, amount):
        """Withdraws the given amount.
        Returns None if successful, or an
        error message if unsuccessful."""
        if amount &lt; 0:
            return 'Amount must be &gt;= 0'
        elif self._balance &lt; amount:
            return 'Insufficient funds'
        else:
            self._balance -= amount
            return None
    def computeInterest(self):
        """Computes, deposits, and returns the interest."""
        interest = self._balance * SavingsAccount.RATE
        self.deposit(interest)


def main():
    bank = Bank()
    bank.add(SavingsAccount("Zelda","1003",5000.00))
    bank.add(SavingsAccount("Wilma","1001",4000.00))
    bank.add(SavingsAccount("Fred","1002",1000.00))
    print bank

main()
</code></pre>
