0

Ok so I need create something that updates a (global variable)bank account. My instructions are as follows:

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

So that when I do something like this;

deposit ("23-12-2012",  "Income", 225)
withdraw("24-12-2012", "Presents", 99.02)
printBalance()

It'll return;

23-12-2012  Income              $  225.00       $  575.01
24-12-2012  Presents            $   99.02       $  475.99
Current Balance is $  475.99

I'm not sure how to do this. At the moment I'm defining printLedgerLine to just get the information and print it with tabs so it looks nice. I'm then calling it within either withdraw or deposit, and trying to print the (new) balance at the end.

def deposit(date, details, amount):
    global balance
    balance = float(balance) + float(amount)
    printLedgerLine(date,amount,details)
    print(str(balance)

This works but prints them on separate lines, as calling the function prints it,then it prints the balance. And if I alter the last 2 lines to

 print(printLedgerLine(date,amount,details),str(balance))

then it prints None balance.

Should I instead return printLedgerLine, but if i do that how do I 'format' it without printing?Frustrating, feels like it's right there and I'm going to be embarrassed by how easy it is! Sorry for the long confusing questions, hard to explain when your an amateur! Thanks

3
  • By doing print(printLedgerLine(date,amount,details),str(balance)) you print the return value of printLedgetLine, which is probably None. How does you printLedgerLine function look? Does is only print the tab-separated lines, or also the "Current Balance" line? Or just "Current Balance" without the actual balance? Commented May 13, 2015 at 8:31
  • cheers both answers! The format was what T needed, before that I was just printing it. @tobias_k - the printLedgerLine function didnt involvethe balance originally, it was only when the deposit/withdrawal was used that it was needed Commented May 13, 2015 at 9:09
  • From the description # with items (and the balance) spaced and formatted it seems that printLedgerLine should always print the balance, so it should be part of that function, not of withdraw or deposit Commented May 13, 2015 at 9:11

2 Answers 2

2

Instead of print(printLedgerLine(date,amount,details),str(balance))

You could do change printLedgerLine to a getLedgerLine and make it return the value, for example

def getLedgerLine(date, amount, details):
    return '{0}\t{1}\t{2}'.format(date, amount, details)

and then use

print('{0}\t{1}'.format(printLedgerLine(date,amount,details),str(balance)))

Or you could probably replace the whole printLedgerLine function:

def deposit(date, details, amount):
    global balance
    balance = float(balance) + float(amount)
    print('{date}\t{amount}\t{details}\t{balance}'.format(
        date=date,
        amount=amount,
        details=details,
        balance=balance
    ))
Sign up to request clarification or add additional context in comments.

Comments

1

Difficult to tell what exactly is wrong without seeing your printLedgerLine function, but from your question it seems that you only print the date, details and amount without the final balance, and print the balance in the deposit and withdraw functions instead.

Instead, you should just move that to printLedgerLine and just call that function in deposit and withdraw. Something like this:

def deposit(date, details, amount):
    global balance
    balance = float(balance) + float(amount)
    printLedgerLine(date,amount,details)

def printLedgerLine(date,amount,details):
    print("{}\t{}\t$ {:.02f}\t$ {:.02f}".format(date, details, amount, balance))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.