Need help with a simple python program

Thread Solved
Reply

Join Date: Aug 2005
Posts: 5
Reputation: cleblanc is an unknown quantity at this point 
Solved Threads: 0
cleblanc cleblanc is offline Offline
Newbie Poster

Need help with a simple python program

 
0
  #1
Aug 20th, 2005
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:


  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'
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,856
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 866
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Need help with a simple python program

 
0
  #2
Aug 20th, 2005
A very interesting problem! Just out of curiosity, where is the module normalDate from?
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5
Reputation: cleblanc is an unknown quantity at this point 
Solved Threads: 0
cleblanc cleblanc is offline Offline
Newbie Poster

Re: Need help with a simple python program

 
0
  #3
Aug 20th, 2005
Not sure, I found it on the web but can't remember where.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: Need help with a simple python program

 
0
  #4
Aug 20th, 2005
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.
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5
Reputation: cleblanc is an unknown quantity at this point 
Solved Threads: 0
cleblanc cleblanc is offline Offline
Newbie Poster

Re: Need help with a simple python program

 
0
  #5
Aug 20th, 2005
It is a homework assignment.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5
Reputation: cleblanc is an unknown quantity at this point 
Solved Threads: 0
cleblanc cleblanc is offline Offline
Newbie Poster

Re: Need help with a simple python program

 
0
  #6
Aug 21st, 2005
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:

  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'
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: Need help with a simple python program

 
0
  #7
Aug 22nd, 2005
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:
  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:
  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:
  1. f.close()
Then re-open it with the type of access you need.
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5
Reputation: cleblanc is an unknown quantity at this point 
Solved Threads: 0
cleblanc cleblanc is offline Offline
Newbie Poster

Re: Need help with a simple python program

 
0
  #8
Aug 22nd, 2005
Got it thanks!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC