I'm completely new to python and really need help with this. I have written this basic cash fund program that allows a person to withdraw, deposit, and receive a balance from it. But now I need to now add email capabilities, by setting up a database (or list) of account holders' information such as email and other information. When a query is made by a user, they should enter a username or some other means of identification. When the query is accomplished, an email verification of the details should also be sent to the user. Please help! Here is my code so far:

from normalDate import ND
today = ND()

class Account:
    def __init__(self, initial):
        self.balance = initial
    def deposit(self, amt):
        self.balance = self.balance + amt
    def withdraw(self, amt):
        self.balance = self.balance - amt
    def getbalance(self):
        return self.balance
    

a = Account( 1000.00 )
inputing = True

while inputing:
    print "Welcome to the Firm's Petty Cash Fund"
    print 
    print '1) Deposit' 
    print '2) Withdraw'
    print '3) Balance'
    print '4) Done'
    print
    action = int(raw_input('Please choose the number next to action you wish to perform: '))            
    if action == 1:
        print
        d = float(raw_input('Enter the amount the amount you have deposited: '))
        print 'You have deposited:', '$',d, 'on', today.formatUS()
        a.deposit(d)
        print 'Your balance is now:', a.getbalance() 
        fill = input('Press Enter to enter to the main menu: ')
            
    elif action == 2:
        print
        w = float(raw_input('Enter the amount that you have withdrawn: '))
        print 'You have withdrawn:', '$',w, 'on', today.formatUS()
        a.withdraw(w)
        print 'Your balance is now:', '$',a.getbalance()
        fill = input('Press Enter to enter to the main menu: ')
        
    elif action == 3:
        print 
        print 'The current balance for', today.formatUS(), 'is', '$',a.getbalance()
        fill = input('Press Enter to enter to the main menu: ') 
        
    else:
        print 'Thank you!'
        inputing = False
else:
    print 'done'

A very interesting problem! Just out of curiosity, where is the module normalDate from?

Not sure, I found it on the web but can't remember where.

Hi cleblanc,

I need more information about how this program will be used if I am to provide useful information myself.

First off, is this program going to be used by people, or is it a mock-up/homework assignment?

If it's going to be used by people, it's not necessarily a "basic" program. How are the users going to access it? Is it going to be a webapp? Is it going to be a client-server program? If so, will the client have a GUI or a command-line interface? How will you keep me from logging in as someone else and stealing from them? If you use passwords, how will you secure them? Do you intend to let people have jointly-held accounts? If so, you need to worry about synchronization issues. Have you created a MySQL database to house account information? An SMTP server to send email from? Will you prevent people from overdrawing on their accounts? Are there two kinds of users (customers and administrators)? If so, your program needs to incorporate a way to distinguish between them. What OS are you using? What other platform information can you supply? And on and on.

If this is just a mock-up, things get a lot simpler. To store account information, I would just use an insecure text file with information "pickled" into it. Instructions on how to use pickle are included in vegaseat's "Starting Python" sticky thread.

Here's what to do: add two more fields (username and email) to your Account class. In the Python shell, import your Account class and create several Account objects, then pickle.dump() them to a text file.

Now that the foundation has been laid, the first thing your program should do is load all the Account objects from the text file via pickle.load(), then ask the user for a username. If it can't find the username in the loaded Account objects, it should terminate with an error message. Otherwise, it should proceed to the rest of the program. Every time you make a change to the balance, you should re-pickle the information.

If you want to send a record of the session by email, you'll need to create some kind of "history" list, such that every time a transaction is made, the history list gets appended to with a text description of what happened. Then, when the user logs out, send the email with the history list in the message.

To send emails, you're likely going to need to use the smtplib and email packages, which means you'll need to set up an SMTP server (or get access to an email account that allows automatic relaying). See here for details on how to use Python to send email; how to set up an SMTP server depends on which OS you're using.

It is a homework assignment.

I tried adding the cPickle module to the code but can't seem to get it to work. I keep getting the following error:
Traceback (most recent call last):
File "C:\eclipse\workspace\External Files\Module 3\CSC113 Module3 SLP\firm's petty cash fund.py", line 20, in ?
load = p.load(f)
IOError: (0, 'Error')

Any help would be appreciated. Here is the code I have so far:

import cPickle as p
from normalDate import ND
today = ND()

class Account:
    def __init__(self, initial):
        self.balance = initial     
    def deposit(self, amt):
        self.balance = self.balance + amt
    def withdraw(self, amt):
        self.balance = self.balance - amt
    def getbalance(self):
        return self.balance

balanceFile = 'accountBalance.data' 
f= open(balanceFile, 'w')
load = p.load(f)
a = Account(load)    
inputing = True

while inputing:
    print "Welcome to the Firm's Petty Cash Fund"
    print 
    print '1) Deposit' 
    print '2) Withdraw'
    print '3) Balance'
    print '4) Done'
    print
    action = int(raw_input('Please choose the number next to action you wish to perform: '))            
    if action == 1:
        print
        d = float(raw_input('Enter the amount the amount you have deposited: '))
        print 'You have deposited:', '$',d, 'on', today.formatUS()
        a.deposit(d)
        print 'Your balance is now:', a.getbalance()
        b = a.getbalance()
        p.dump(b, f)
        f.close() 
        fill = input('Press Enter to enter to the main menu: ')
            
    elif action == 2:
        print
        w = float(raw_input('Enter the amount that you have withdrawn: '))
        print 'You have withdrawn:', '$',w, 'on', today.formatUS()
        a.withdraw(w)
        print 'Your balance is now:', '$',a.getbalance()
        b = a.getbalance()
        p.dump(b, f)
        f.close()
        fill = input('Press Enter to enter to the main menu: ')
        
    elif action == 3:
        print 
        print 'The current balance for', today.formatUS(), 'is', '$',a.getbalance()
        b = a.getbalance()
        p.dump(b, file)
        fill = input('Press Enter to enter to the main menu: ') 
        
    else:
        print 'Thank you!'
        inputing = False
else:
    print 'done'

Hi cleblanc,

It looks like you're trying to load from a file object that is opened for write-access. This is the code I'm talking about:

balanceFile = 'accountBalance.data' 
f= open(balanceFile, 'w')
load = p.load(f)

I don't know how cPickle works, because I've only ever used pickle, but with pickle.load() you need to specify a file object that has been opened with read-access, like so:

balanceFile = 'accountBalance.data' 
f= open(balanceFile, 'r')
load = p.load(f)

But beware! If the file is opened for read-access, you can't write to it without closing it first, and vice versa. To close a file object, you need to say:

f.close()

Then re-open it with the type of access you need.

Got it thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.