Simple account program

M.E.Farmer mefjr75 at hotmail.com
Thu Mar 17 16:12:34 EST 2005


Igorati,
I wished I could give you a simple fix, BUT...
You need to really re-read the docs and do some tutors first .
Your understanding of classes and namespaces is flawed and will not
become clear without futher study.

search strategy:
python namespaces
python class
python tutor

Classes are like containers.
Classes have there own namespace, "self".
Multiple classes do not share namespaces.
Stay away from inheritance till you have an understanding of namespace,
no need in getting mixed up.
Withdrawl, Deposit, Transaction should be members of the account class.
That way they can share a namespace and simplify your design.
Why do you need to search a pickle?
Pickle an instance of the class and unpickle it when needed and your
data will be however it was in your instance.
If you have real code post it because this code does not work and never
will.
Quit posting homework, you can ask for occasional help, but this is the
third time i have seen this, it is getting worse and you have been
warned before ;)
Surely in the month since you first asked you could have read a bit of
documentation.
# this is not complete or even tested it is just an example ( based on
your code )
import time
class Account:
    def __init__(self, initial):
         self.balance = initial
         self.history = {}

    def deposit(self, amt):
         self.balance = self.balance + amt
         self.save('deposit',amt)

    def withdraw(self, amt):
         self.balance = self.balance - amt
         self.save('withdrawl', amt)

    def getbalance(self):
         return self.balance

    def gethistory(self):
        return self.history

    def save(self,trans,amount):
        # store the transaction type, amount, balance
        self.history[self.timestamp()] = (trans,amount,self.balance)

    def timestamp(self):
         return time.asctime()

    def transaction(self):
        withdrawl = raw_input("Is this a deposit or withdrawl?")
        amount = raw_input("Please enter amount here.")
        if withdrawl.lower() in ["withdrawl","w"]:
            self.withdraw(amount)
        else:
            self.deposit(amount)
hth,
M.E.Farmer




More information about the Python-list mailing list