Group A

Q4: 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.

Bank Transactions

Solution and implementation for Q4 from Data Structures Laboratory (dsl).

grpA_4.py Download
def deposit(balance, amount):
    return balance + amount

def withdraw(balance, amount):
    if balance >= amount:
        return balance - amount
    else:
        print("Withdrawal denied: Insufficient balance.")
        return balance

balance = 0

transaction_log = input("Enter transaction log (e.g., 'D 100, W 200'): ")
transactions = transaction_log.split(", ")

for transaction in transactions:
    action, amount = transaction.split()
    amount = int(amount)
    
    if action == 'D':
        balance = deposit(balance, amount)
    elif action == 'W':
        balance = withdraw(balance, amount)
    else:
        print(f"Invalid transaction type '{action}'. Use 'D' for deposit or 'W' for withdrawal.")

print("Total Balance:", balance)

Other Questions in Data Structures Laboratory

See All Available Questions
Download