#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    3
    Rep Power
    0

    Totally new and going crazy...


    I'm really very very new to Python and I'm trying to design a program that will keep track of a small offices petty cash fund. It needs to keep some record of transactions and the program needs to be capable of referencing that list of transactions. This is the code I have and for the life of me I can't figure out what I'm doing wrong.

    Code:
    import datetime
    import classes2
    a = Account(1000.00)
    print "Welcome to the office petty fund management program!"
    depoz = raw_input("Would you like to make a deposit?")
        if answer == 'YES':
            enter1 = raw_input("How much would you like to deposit?")
            a.deposit(enter1)
        if answer == 'NO':
            enter1 = raw_input("Would you like to make a withdrawl?")
            a.withdraw(enter1)
    balance = raw_input("Would you like to see the balance?")
        if answer == 'YES':
            print a.getbalance
        if answer == 'NO':
            break
    tranz = raw_input("Would you like to see the transaction records?")
        if answer == 'YES':
            print a.transactions
        if answer == 'NO':
            break
    Thats the orginial script, here's the module:
    Code:
    class Account:
    	def __init__(self, initial):
    		self.balance = initial
    	def deposit(self, amt):
    		self.balance = self.balance + amt
    		x = datetime.date.today
    		actions = open( "actions.txt" , "a")
    		actions
    		actions.write("Deposit:" (str(x)) + " " + (str(enter1)))
    		actions.close
    	def withdraw(self,amt):
    		self.balance = self.balance - amt
    		x = datetime.date.today
    		actions = open( "actions.txt" , "a")
    		actions
    		actions.write("Withdrawal:" (str(x)) + " " + (str(enter1)))
    		actions.close
    	def getbalance(self):
    		return self.balance
    	def transactions(self):
    		actions = open( "actions.txt" , "r")
    		actions
    		actions.seek(0)
    		actions.close
    		z = actions.read()
    		return z
    Thanks for your help guys, any advice to get this to work would be great.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Code:
    depoz = raw_input("Would you like to make a deposit?")
        if answer == 'YES':
            enter1 = raw_input("How much would you like to deposit?")
            a.deposit(enter1)
        if answer == 'NO':
    I think you should be looking where the value in 'answer' comes from... (i.e. nowhere )

    Instead, try:

    Code:
    depoz = raw_input("Would you like to make a deposit?")
        if depoz == 'YES':
            enter1 = raw_input("How much would you like to deposit?")
            a.deposit(enter1)
        if depoz == 'NO':
    And you've done the same thing the other times when you've asked for 'balance' and 'tranz' from raw_input(), then checked 'answer' instead on the next line.

    Your file reading code:
    Code:
    actions = open( "actions.txt" , "r")
    actions
    actions.seek(0)
    actions.close
    z = actions.read()
    return z
    Could be trimmed to

    Code:
    return file("actions.txt" , "r").readlines()
    But at a glance, it looks like it should work if you change the 'answer' lines.


    (You might consider handling them more like:

    Code:
    depoz = depoz.lower()
    if depoz in ["y", "yes", "yeah"]:
        ...
    if depoz in ["n", "no", "nope"]:
        ...
    to save you having to type the whole YES or NO in capital letters each time).
    Last edited by sfb; June 14th, 2005 at 07:40 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    3
    Rep Power
    0
    Thanks man, good eye spy. I swaped the answer for its appropriate value, I'm confused however what you mean by the lower case y, yes, and yeah answers. Any way you could elaborate more?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    24
    he was meaning instead of it just being yes or no you could have the user answer y, yes, yeah to for the question and the computer would know what to do. Just makes it easer on the user.
  8. #5
  9. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    when i made programs like that i always did something like "enter 1 for yes and 2 for no"... made it easier
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by Violent Dave
    Thanks man, good eye spy. I swaped the answer for its appropriate value, I'm confused however what you mean by the lower case y, yes, and yeah answers. Any way you could elaborate more?
    In sbfs example he used the string types lower() method will convert the result of the raw_input() function into lowercase, this simply means that the answer can be in any case and still be valid since it's been compared against values of the same case .

    The use of the in operator really just makes it easier to check multiple values at once. Otherwise you'd have to write an if statement like the one below.

    Code:
    if answer == 'yes' or answer == 'y' or answer == 'yeah'...
    This isn't so pretty .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    3
    Rep Power
    0
    Awesome, thanks for the advice. I have to use tkinter to convert this particular program to have some sort of GUI. I'm reading up on that and it doesn't seem like it would be all the complex. Although for some reason the prerequisite to this assignment is that I add "exceptions" to the program. Anyone have any helpful recommendations on how I can go about this? Advice on either of the critria would be helpful.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    6
    Rep Power
    0
    Hi Dave,

    I am also nearly new to Python (only perhaps I started before you...).
    I modified your fund management program, founding new solutions (nothing exceptional),
    but I want share with you my modest work, also to show to the true "experts" what is my preferred style of replay.

    To help us newbies, IMHO, the experts have to explain better (giving, if possible, meaningful examples) or leave the answers to someone else.

    The modified program I list here is line-numbered to ease comments.

    01 # fund_management.py
    02
    03 import datetime
    04 import sys, string # sys loaded for exit - string for uooer
    05
    06 class Account:
    07 def __init__(self, initial):
    08 self.balance = initial
    09 def deposit(self, amt):
    10 self.balance = self.balance + amt
    11 x = datetime.date.today() # get the current date
    12 # format aaaa-mm-dd
    13
    14 actions = open( "actions.txt" , "a")
    15 actions.write("Deposit:" +str(x) + " " + (str(nAmount))+'\n')
    16 actions.close
    17 def withdraw(self,amt):
    18 self.balance = self.balance - amt
    19 x = datetime.date.today()
    20 actions = open( "actions.txt" , "a")
    21 actions.write("Withdrawal:"+(str(x)) + " " + (str(nAmount))+'\n')
    22 actions.close
    23 def getbalance(self):
    24 return self.balance
    25 def transactions(self):
    26 actions = open( "actions.txt" , "r")
    27 actions.seek(0)
    28 z = actions.read()
    29 actions.close
    30 return z
    31
    32
    33 a = Account(1000.00) # class istantiation
    34 # with initialization of a.balance to 1000.00
    35
    36 print "Welcome to the office petty fund management program!\n"
    37 sInputOptions="""
    38 Would you like to make a deposit (d)
    39 withdraw (w)
    40 or see the:
    41 balance (b)
    42 transactions (t)?
    43
    44 (x to exit)
    45 """
    46
    47 sOption = raw_input(sInputOptions)
    48 # to take into account the possibility of shifted answers:d
    49 # sOption = string.upper(sOption)
    50 # or (without string module):
    51 # sOption=sOption.upper()
    52
    53 if sOption not in "dwbtxDWBTX":
    54 print "Error in options!\n\n"
    55 print "accepted options = dwbtx or DWBTX"
    56 print ' --> Deposit - Withdraw - Balance - Transactions - eXit'
    57 sys.exit(-1)
    58
    59 # still another solution: test for both (shifted or not)
    60 if sOption in 'dD':
    61 nAmount=float(raw_input("How much would you like to deposit? "))
    62
    63 a.deposit(nAmount)
    64 elif sOption in 'wW':
    65 nAmount=float(raw_input("How much would you like to withdraw?"))
    66 elif sOption in' bB':
    67 print a.getbalance()
    68 elif sOption in 'tT':
    69 print a.transactions()
    70 elif sOption in 'xX':
    71 sys.exit(-1)
    72
    73 # print a.getbalance()
    74 # print a.transactions()

    As you can see, I left almost unchanged your Class Account (you know OO better than me!).
    line 37: when there are multiple options, I think the best is to use a triple string for the question.
    line 53: test the correctness of the option choosed by user
    There are various solutions: trasform all in shifted case (upper) or test for the two versions (shifted or not)

    line 60: sOption elaboration

    I could go deeper in commenting, but my code doesn't deserve too much...
    An advice: pay attention to variables and indentation!
    If you or others find some mistakes ... it's normal!
    Anyway feel free to ask if you don't understand something or you disagree with my approch...
    Bye.

    PS - To eliminate the line numbering use a cut/paste in column or, if you use Linux,:
    cat file|cut -c 4- > newfile
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    6
    Rep Power
    0
    Hi Dave,
    ... and I told you "Pay attention to indentation"!
    I send again the program because there has been some problem...
    Code:
    01 # fund_management.py
    02 
    03 import datetime 
    04 import sys, string    # sys loaded for exit - string for uooer
    05 
    06 class Account:
    07     def __init__(self, initial):
    08         self.balance = initial
    09     def deposit(self, amt):
    10         self.balance = self.balance + amt
    11         x = datetime.date.today() # get the current date
    12                                   # format aaaa-mm-dd
    13         
    14         actions = open( "actions.txt" , "a")
    15         actions.write("Deposit:" +str(x) + " " + (str(nAmount))+'\n')
    16         actions.close
    17     def withdraw(self,amt):
    18         self.balance = self.balance - amt
    19         x = datetime.date.today()
    20         actions = open( "actions.txt" , "a")
    21         actions.write("Withdrawal:"+(str(x)) + " " + (str(nAmount))+'\n')
    22         actions.close
    23     def getbalance(self):
    24         return self.balance
    25     def transactions(self):
    26         actions = open( "actions.txt" , "r")
    27         actions.seek(0)
    28         z = actions.read()
    29         actions.close
    30         return z   
    31     
    32 
    33 a = Account(1000.00)  # class istantiation \
    34                       # with initialization of a.balance to 1000.00
    35                       
    36 print "Welcome to the office petty fund management program!\n"
    37 sInputOptions="""
    38 Would you like to make a   deposit      (d)  
    39                            withdraw     (w)
    40                or see the:
    41                            balance      (b)
    42                            transactions (t)?
    43 
    44  (x to  exit)
    45 """
    46 
    47 sOption = raw_input(sInputOptions)
    48 # to take into account the possibility of shifted answers:d
    49 # sOption = string.upper(sOption)  
    50 # or (without string module):
    51 # sOption=sOption.upper()
    52 
    53 if sOption not in "dwbtxDWBTX":    
    54     print "Error in options!\n\n"
    55     print "accepted options = dwbtx or DWBTX"
    56     print '           -->     Deposit - Withdraw - Balance - Transactions - eXit'
    57     sys.exit(-1)
    58 
    59 # still another solution: test for both (shifted or not)
    60 if sOption in 'dD':
    61     nAmount=float(raw_input("How much would you like to deposit? "))
    62 
    63     a.deposit(nAmount)
    64 elif sOption in 'wW':
    65     nAmount=float(raw_input("How much would you like to withdraw?"))
    66 elif sOption in' bB':
    67     print a.getbalance()
    68 elif sOption in 'tT':
    69     print a.transactions()     
    70 elif sOption in 'xX':
    71     sys.exit(-1)    
    72 
    73 # print a.getbalance()
    74 # print a.transactions()
    Bye.

IMN logo majestic logo threadwatch logo seochat tools logo