Practical No: -3
Experiment No. 2: Write a Python program that computes the net amount of a bank account based a
transaction log from console input. The transaction log format is shown as following: D 100 W 200
(Withdrawal is not allowed if balance is going negative. Write functions for withdraw and deposit) D
means deposit while W means withdrawal.
Suppose the following input is supplied to the program:
D 300
D 300
W 200
D 100
Then, the output should be: 500
Code:
l1=[]
bal=0
def menu():
print("\n Enter Transaction type:")
print("\n W-Withdraw amount.\n D-Deposit amount: \n B-Stop")
def createLog(bal):
menu()
ch=input("\n Enter ur choice:")
if(ch=="B"):
quit()
if(ch=="W"):
print("\n Enter amount to withdraw:")
amt=int(input("Amount:"))
#condition bal must be greater than withdraw
if(bal >= amt):
l1.append("W "+ str(amt))
bal = bal-amt
else:
print("\n Transaction failure")
if(ch=="D"):
print("\n Enter amount to deposite:")
amt=int(input("Amount:"))
l1.append("D "+ str(amt))
bal=bal+amt
return(bal)
def viewlog():
print(l1)
def balance():
print("Balance:",bal)
bal=0
while(1):
bal=createLog(bal)
balance()
viewlog()
Output:
Enter Transaction type:
W-Withdraw amount.
D-Deposit amount:
B-Stop
Enter ur choice:D
Enter amount to deposite:
Amount:300
Balance: 300
['D 300']
Enter Transaction type:
W-Withdraw amount.
D-Deposit amount:
B-Stop
Enter ur choice:D
Enter amount to deposite:
Amount:300
Balance: 600
['D 300', 'D 300']
Enter Transaction type:
W-Withdraw amount.
D-Deposit amount:
B-Stop
Enter ur choice:W
Enter amount to withdraw:
Amount:200
Balance: 400
['D 300', 'D 300', 'W 200']
Enter Transaction type:
W-Withdraw amount.
D-Deposit amount:
B-Stop
Enter ur choice:D
Enter amount to deposite:
Amount:100
Balance: 500
['D 300', 'D 300', 'W 200', 'D 100']