944,085 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 5696
  • Python RSS
Aug 20th, 2005
0

Need help with a simple python program

Expand Post »
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:


Python Syntax (Toggle Plain Text)
  1. from normalDate import ND
  2. today = ND()
  3.  
  4. class Account:
  5. def __init__(self, initial):
  6. self.balance = initial
  7. def deposit(self, amt):
  8. self.balance = self.balance + amt
  9. def withdraw(self, amt):
  10. self.balance = self.balance - amt
  11. def getbalance(self):
  12. return self.balance
  13.  
  14.  
  15. a = Account( 1000.00 )
  16. inputing = True
  17.  
  18. while inputing:
  19. print "Welcome to the Firm's Petty Cash Fund"
  20. print
  21. print '1) Deposit'
  22. print '2) Withdraw'
  23. print '3) Balance'
  24. print '4) Done'
  25. print
  26. action = int(raw_input('Please choose the number next to action you wish to perform: '))
  27. if action == 1:
  28. print
  29. d = float(raw_input('Enter the amount the amount you have deposited: '))
  30. print 'You have deposited:', '$',d, 'on', today.formatUS()
  31. a.deposit(d)
  32. print 'Your balance is now:', a.getbalance()
  33. fill = input('Press Enter to enter to the main menu: ')
  34.  
  35. elif action == 2:
  36. print
  37. w = float(raw_input('Enter the amount that you have withdrawn: '))
  38. print 'You have withdrawn:', '$',w, 'on', today.formatUS()
  39. a.withdraw(w)
  40. print 'Your balance is now:', '$',a.getbalance()
  41. fill = input('Press Enter to enter to the main menu: ')
  42.  
  43. elif action == 3:
  44. print
  45. print 'The current balance for', today.formatUS(), 'is', '$',a.getbalance()
  46. fill = input('Press Enter to enter to the main menu: ')
  47.  
  48. else:
  49. print 'Thank you!'
  50. inputing = False
  51. else:
  52. print 'done'
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cleblanc is offline Offline
5 posts
since Aug 2005
Aug 20th, 2005
0

Re: Need help with a simple python program

A very interesting problem! Just out of curiosity, where is the module normalDate from?
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Aug 20th, 2005
0

Re: Need help with a simple python program

Not sure, I found it on the web but can't remember where.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cleblanc is offline Offline
5 posts
since Aug 2005
Aug 20th, 2005
0

Re: Need help with a simple python program

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.
Reputation Points: 41
Solved Threads: 31
Junior Poster
G-Do is offline Offline
146 posts
since Jun 2005
Aug 20th, 2005
0

Re: Need help with a simple python program

It is a homework assignment.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cleblanc is offline Offline
5 posts
since Aug 2005
Aug 21st, 2005
0

Re: Need help with a simple python program

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:

Python Syntax (Toggle Plain Text)
  1. import cPickle as p
  2. from normalDate import ND
  3. today = ND()
  4.  
  5. class Account:
  6. def __init__(self, initial):
  7. self.balance = initial
  8. def deposit(self, amt):
  9. self.balance = self.balance + amt
  10. def withdraw(self, amt):
  11. self.balance = self.balance - amt
  12. def getbalance(self):
  13. return self.balance
  14.  
  15. balanceFile = 'accountBalance.data'
  16. f= open(balanceFile, 'w')
  17. load = p.load(f)
  18. a = Account(load)
  19. inputing = True
  20.  
  21. while inputing:
  22. print "Welcome to the Firm's Petty Cash Fund"
  23. print
  24. print '1) Deposit'
  25. print '2) Withdraw'
  26. print '3) Balance'
  27. print '4) Done'
  28. print
  29. action = int(raw_input('Please choose the number next to action you wish to perform: '))
  30. if action == 1:
  31. print
  32. d = float(raw_input('Enter the amount the amount you have deposited: '))
  33. print 'You have deposited:', '$',d, 'on', today.formatUS()
  34. a.deposit(d)
  35. print 'Your balance is now:', a.getbalance()
  36. b = a.getbalance()
  37. p.dump(b, f)
  38. f.close()
  39. fill = input('Press Enter to enter to the main menu: ')
  40.  
  41. elif action == 2:
  42. print
  43. w = float(raw_input('Enter the amount that you have withdrawn: '))
  44. print 'You have withdrawn:', '$',w, 'on', today.formatUS()
  45. a.withdraw(w)
  46. print 'Your balance is now:', '$',a.getbalance()
  47. b = a.getbalance()
  48. p.dump(b, f)
  49. f.close()
  50. fill = input('Press Enter to enter to the main menu: ')
  51.  
  52. elif action == 3:
  53. print
  54. print 'The current balance for', today.formatUS(), 'is', '$',a.getbalance()
  55. b = a.getbalance()
  56. p.dump(b, file)
  57. fill = input('Press Enter to enter to the main menu: ')
  58.  
  59. else:
  60. print 'Thank you!'
  61. inputing = False
  62. else:
  63. print 'done'
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cleblanc is offline Offline
5 posts
since Aug 2005
Aug 22nd, 2005
0

Re: Need help with a simple python program

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:
Python Syntax (Toggle Plain Text)
  1. balanceFile = 'accountBalance.data'
  2. f= open(balanceFile, 'w')
  3. 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:
Python Syntax (Toggle Plain Text)
  1. balanceFile = 'accountBalance.data'
  2. f= open(balanceFile, 'r')
  3. 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:
Python Syntax (Toggle Plain Text)
  1. f.close()
Then re-open it with the type of access you need.
Reputation Points: 41
Solved Threads: 31
Junior Poster
G-Do is offline Offline
146 posts
since Jun 2005
Aug 22nd, 2005
0

Re: Need help with a simple python program

Got it thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cleblanc is offline Offline
5 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: how to do two things at once?
Next Thread in Python Forum Timeline: Testing the Psyco Module to Speed Up Python





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC