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
print(printLedgerLine(date,amount,details),str(balance))you print the return value ofprintLedgetLine, which is probablyNone. How does youprintLedgerLinefunction look? Does is only print the tab-separated lines, or also the "Current Balance" line? Or just "Current Balance" without the actual balance?# with items (and the balance) spaced and formattedit seems thatprintLedgerLineshould always print the balance, so it should be part of that function, not ofwithdrawordeposit