Back

Transaction Anlayzer

The code processes a list of financial transactions, allowing the user to print a summary, analyze transactions, or stop the session.

Code Block

The script defines three main functions: print_transactions, print_summary, and analyze_transactions. The print_transactions function iterates through a list of transactions, printing each amount and its associated statement. The print_summary function calculates the total deposits, total withdrawals, and the final balance. The analyze_transactions function sorts the transactions, identifies the largest withdrawal and deposit, and calculates the average deposit and withdrawal amounts. The user is prompted to choose between printing a summary, analyzing transactions, or stopping the session.

Check out the Code

Code Analysis

Fixing Sorting Issue:

  • The analyze_transactions function sorts transactions in ascending order based on amounts. However, since deposits and withdrawals are mixed, this can result in incorrect identification of the largest deposit or withdrawal. Sort deposits and withdrawals separately.

def analyze_transactions(transactions):
   deposits = [act for act in transactions if act[0] >= 0]
   withdrawals = [act for act in transactions if act[0] < 0]
   largest_deposit = max(deposits, key=lambda x: x[0], default=(0, "No Deposits"))
   largest_withdrawal = min(withdrawals, key=lambda x: x[0], default=(0, "No Withdrawals"))
   print(f"Largest withdrawal: {largest_withdrawal}, Largest deposit: {largest_deposit}")

Improving the Summary Output:

  • The print_summary function currently prints the total deposited, total withdrawn, and balance without clear labels. Adding labels will make the output more readable.

def print_summary(transactions):
   deposits = [act[0] for act in transactions if act[0] >= 0]
   total_deposited = sum(deposits)
   print(f"Total Deposited: ${total_deposited:.2f}")

   withdrawals = [act[0] for act in transactions if act[0] < 0]
   total_withdrawn = sum(withdrawals)
   print(f"Total Withdrawn: ${total_withdrawn:.2f}")

   balance = total_deposited + total_withdrawn
   print(f"Balance: ${balance:.2f}")

Handling Edge Cases:

  • If there are no deposits or withdrawals, the current calculation of averages could lead to a division by zero. Include a check for this case.

def analyze_transactions(transactions):
   deposits = [act[0] for act in transactions if act[0] >= 0]
   total_deposit = sum(deposits)
   average_deposit = total_deposit / len(deposits) if deposits else 0
   print(f"Average deposit: ${average_deposit:.2f}")

   withdrawals = [act[0] for act in transactions if act[0] < 0]
   total_withdrawals = sum(withdrawals)
   average_withdrawal = total_withdrawals / len(withdrawals) if withdrawals else 0
   print(f"Average withdrawal: ${average_withdrawal:.2f}")

   if deposits:
       largest_deposit = max(deposits)
   else:
       largest_deposit = 0
   if withdrawals:
       largest_withdrawal = min(withdrawals)
   else:
       largest_withdrawal = 0

   print(f"Largest withdrawal: ${largest_withdrawal:.2f}")
   print(f"Largest deposit: ${largest_deposit:.2f}")

Improving User Interface:

  • The while loop can be enhanced to provide more user-friendly feedback and clear instructions.

while True:
   choice = input("Would you like to print, analyze, or stop? (print/analyze/stop): ").strip().lower()
   if choice == "print":
       print_summary(data)
   elif choice == "analyze":
       analyze_transactions(data)
   elif choice == "stop":
       print("You have cancelled your session.")
       break
   else:
       print("Invalid choice. Please type 'print', 'analyze', or 'stop'.")

Separation of Concerns:

  • Separate the input handling from the transaction logic to make the code more modular and easier to test. For instance, move the user interaction code out of the main loop into a separate function.

Handling Empty Data:

  • Before processing, check if the data list is empty to avoid errors and provide meaningful output when there are no transactions to analyze.

Final Notes:

This code successfully processes and analyzes a list of financial transactions. By improving sorting logic, enhancing the user interface, and refining the summary and analysis functions, you can make the script more accurate, user-friendly, and robust. Additionally, ensuring proper handling of edge cases and separating concerns will contribute to better maintainability and flexibility in future developments.