In: Computer Science
In accounting, balance sheets are used to keep of track how much money a person has in an account. Write a Python program to create a simple three column balance sheet, showing credits in one column, debits in the next, and a running balance in the third. If the balance dips into the negative, you must show the value in parentheses, the standard method in accounting. Input will be from a text file. The first value will be the opening balance in the account, followed by an integer N indicating N sets of data to follow. Each subsequent data set will be either the letter 'C' for credit, or 'D' for debit, followed by a dollar amount. Output to the screen a complete balance sheet, with headings and boundary lines above and below, and columns 14 spaces wide. All amounts will be at least one dollar and are guaranteed to "fit" in the balance sheet. All values with four digits or more in the whole number portion are to be output with "comma " notation, and all negative values in the balance column are to be shown in parentheses.
Let the user input the file name from the keyboard. Refer to the sample output below.
Sample File:
999.99
5
C 8250.55
D 12397.45
D 12345.67
C 15492.59
C 5000000
Sample Run:
Enter file name: account.txt DEBIT CREDIT BALANCE -------------*-------------*-------------* $ 999.99 $ 8,250.55 $ 9,250.54 $ 12,397.45 $ (3,146.91) $ 12,345.67 $ (15,492.58) $ 15,492.59 $ 0.01 $5,000,000.00 $5,000,000.01 -------------*-------------*-------------* |
Solution:
Look at the code and comments for better understanding........
Screenshot of the code:
Output:
Code to copy:
#reading the filename from the user
filename = input("Enter file name : ")
file = open(filename)
#reading the first line from the file i.e balance
balance = float(file.readline())
#reading second line from the file i.e number of entries
N = int(file.readline())
#printing the table header
print(" DEBIT CREDIT BALANCE")
print(('-------------*')*3)
print("\t\t\t\t$ %.2f"%balance)
#looping upto N times
for i in range(N):
#readin the line from the file and spliting it
action,amount = file.readline().split()
amount = float(amount)
#for Credit amount
if action=='C':
balance += amount
print('\t\t$ {:,.2f}\t$ {:,.2f}'.format(amount,balance))
#for debit amount
else:
balance -= amount
#if the balance is negative
if(balance<0):
print('$ {:,.2f}\t\t\t$ ({:,.2f})'.format(amount,-balance))
else:
print('$ {:,.2f}\t\t\t$ {:,.2f}'.format(amount,balance))
print(('-------------*')*3)
I hope this would help..............................:-))