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

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2

    Question Need urgent help with Withdraw/Deposit functions for account balance in Python


    I am supposed to complete the following five functions in order to create a running account balance. I have done the first two functions.
    I dont how to create the deposit and withdraw functions so that balance=balance-withdraw..Pleas help!


    def setBalance(amt): # Defines (but doesn't print) the value of the account balance

    def printBalance(): # Displays current balance as a money value with a heading

    def printLedgerLine(date, amount, details): # with items (and the balance) spaced and formatted

    def deposit (date, details, amount): # Alter the balance and print ledger line

    def withdraw(date, details, amount): # Alter the balance and print ledger line
    Your task is to:

    This is how it will be called:
    setBalance(500)
    printBalance()
    withdraw("17-12-2012", "BP - petrol", 72.50)
    withdraw("19-12-2012", "Countdown", 55.50)
    withdraw("20-12-2012", "munchies", 1.99)
    withdraw("22-12-2012", "Vodafone", 20)
    deposit ("23-12-2012", "Income", 225)
    withdraw("24-12-2012", "Presents", 99.02)
    printBalance()


    What i done so far:
    g_balance = 0


    def setBalance(amount):
    global g_balance
    if amount:
    g_balance = amount


    def printBalance():
    global g_balance
    if g_balance:
    printNow("%s $%0.2f" % ("Currrent balance is",g_balance))

    deposit=0
    def deposit (amount):
    global g_balance
    balance = g_balance + deposit
    if deposit < g_balance:
    return balance

    I dont how to create the deposit and withdraw functions so that balance=balance-withdraw..Pleas help!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    You were given the interface to the deposit function, and that it should change the global balance.
    Code:
    def deposit (date, details, amount): # Alter the balance and print ledger line
    Now, when some other code calls deposit, say as in the example:

    deposit ("23-12-2012", "Income", 225)

    the local variables `date' will have '23-12-2012', the value of `details' will have 'Income' and `amount' will have a number 225 .

    Calling the function with appropriate argument values is the way for information to enter the function.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by b49P23TIvg
    You were given the interface to the deposit function, and that it should change the global balance.
    Code:
    def deposit (date, details, amount): # Alter the balance and print ledger line
    Now, when some other code calls deposit, say as in the example:

    deposit ("23-12-2012", "Income", 225)

    the local variables `date' will have '23-12-2012', the value of `details' will have 'Income' and `amount' will have a number 225 .

    Calling the function with appropriate argument values is the way for information to enter the function.

    def deposit (date, details, amount):
    global g_balance
    printNow("%f %s %f" % ("date","income",amount))
    balance = g_balance + deposit


    Would it be something like this? or do i need to use raw_input?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    Closer. Use the python interpreter to test python.
    Code:
    def deposit (date, details, amount):
        global g_balance
        printNow("%f %s %f" % ("date","income",amount))
        balance = g_balance + deposit
    What is printNow? Answers: you're trying to program like the infinite number of monkeys writing a novel by typing for infinite time.

    Why would you update balance instead of g_balance?
    balance = g_balance + deposit

    What is the data type of date given the test case? Answer, it's a string. The format specifier %s is correct, not %f.

    Why is "income" as a string format parameter? The point of function arguments is to make functions work with many inputs.

    Maybe you're trying and struggling and reading your text and the text is good, and your efforts are your honest best. I doubt this scenario.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo