SE Computer Engineering practical 3 solution

SE Computer Engineering Practical 3 solution

Question:

1. Write a Python program that computes the net amount of a bank account based on 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:

# computes net bank amount based on the input
# "D" for deposit, "W" for withdrawal

# define a variable for main amount
net_amount = 0

while True:
   # input the transaction
   str = input("Enter transaction: ")

   # get the value type and amount to the list
   # separated by space
   transaction = str.split(" ")

   # get the value of transaction type and amount
   # in the separated variables
   type = transaction[0]
   amount = int(transaction[1])

   if type == "D" or type == "d":
       net_amount += amount
   elif type == "W" or type == "w":
       net_amount -= amount
   else:
       pass

   # input choice
   str = input("want to continue (Y for yes) : ")
   if not (str[0] == "Y" or str[0] == "y"):
       # break the loop
       break

# print the net amount
print("Net amount: ", net_amount)

Output:

Enter transaction: D 300
want to continue (Y for yes) : y
Enter transaction: D 300
want to continue (Y for yes) : y
Enter transaction: W 200
want to continue (Y for yes) : y
Enter transaction: D 100
want to continue (Y for yes) : n
Net amount:  500

Process finished with exit code 0
Tech Amplifier Final Logo