compare amount paid in interest with/out extra payments

This commit is contained in:
- 2024-02-10 10:58:05 -08:00
parent fdf01baf6a
commit ccb39cfc21

View File

@ -21,6 +21,8 @@ def generate_amortization_schedule(principal, interest_rate, loan_term, extra_pa
one_time_payment = extra_payments.pop(0) one_time_payment = extra_payments.pop(0)
for payment_number in range(1, num_payments + 1): for payment_number in range(1, num_payments + 1):
# Calculate interest for the current period # Calculate interest for the current period
if remaining_balance == 0:
break
interest_payment = round(remaining_balance * monthly_interest_rate, 2) interest_payment = round(remaining_balance * monthly_interest_rate, 2)
# Calculate principal payment # Calculate principal payment
@ -32,7 +34,6 @@ def generate_amortization_schedule(principal, interest_rate, loan_term, extra_pa
remaining_balance -= one_time_payment['amount'] remaining_balance -= one_time_payment['amount']
if extra_payments != []: if extra_payments != []:
one_time_payment = extra_payments.pop(0) one_time_payment = extra_payments.pop(0)
# Update remaining balance # Update remaining balance
remaining_balance -= principal_payment remaining_balance -= principal_payment
@ -50,9 +51,27 @@ def generate_amortization_schedule(principal, interest_rate, loan_term, extra_pa
'Remaining Balance': round(remaining_balance,2) 'Remaining Balance': round(remaining_balance,2)
} }
amortization_schedule.append(payment_details) amortization_schedule.append(payment_details)
return amortization_schedule return amortization_schedule
def get_totals(amortization_schedule, display=False, export=False):
total_paid = 0
total_interest_paid = 0
total_principal_paid = 0
# Display the amortization schedule
if display:
print("id, paid, interest payment, principal payment, remaining")
for payment in amortization_schedule:
# print(payment)
total_paid += payment["Payment Amount"]
total_interest_paid += payment["Interest Payment"]
total_principal_paid += payment["Principal Payment"]
if payment["Remaining Balance"] < 0:
break
attrs = [payment[key] for key in payment]
if display:
print("%s" % ", ".join([str(attr) for attr in attrs]))
return total_paid, total_interest_paid, total_principal_paid
if __name__ == "__main__": if __name__ == "__main__":
# Example usage # Example usage
# principal = 100000 # principal = 100000
@ -82,6 +101,13 @@ if __name__ == "__main__":
extra.sort(key=lambda k: k["payment_number"]) extra.sort(key=lambda k: k["payment_number"])
return args.principal, args.interest_rate, args.term, extra return args.principal, args.interest_rate, args.term, extra
def compare(with_extra_payments, without):
x,y,z = get_totals(with_extra_payments)
a,b,c = get_totals(without)
print(chr(916), "paid: ", round(a-x,2))
print(chr(916), "interest paid: ", round(b-y,2))
print(chr(916), "principal paid: ", round(c-z,2))
principal, interest_rate, loan_term, extra_payments = get_arguments() principal, interest_rate, loan_term, extra_payments = get_arguments()
# one_time_payment = { # one_time_payment = {
@ -90,23 +116,14 @@ if __name__ == "__main__":
# } # }
schedule = generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments) schedule = generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments)
paid, interest_paid, principal_paid = get_totals(schedule, True)
total_paid = 0 print("total paid: ", round(paid,2))
total_interest_paid = 0 print("total interest paid: ", round(interest_paid,2))
total_principal_paid = 0 print("total principal paid: ", round(principal_paid,2))
# Display the amortization schedule # without extra payments for comparison
print("id, paid, interest payment, principal payment, remaining") compare(schedule, generate_amortization_schedule(
for payment in schedule: principal, interest_rate, loan_term)
# print(payment) )
total_paid += payment["Payment Amount"]
total_interest_paid += payment["Interest Payment"]
total_principal_paid += payment["Principal Payment"]
if payment["Remaining Balance"] < 0:
break
attrs = [payment[key] for key in payment]
print("%s" % ", ".join([str(attr) for attr in attrs]))
print("total paid: ", round(total_paid,2))
print("total interest paid: ", round(total_interest_paid,2))
print("total principal paid: ", round(total_principal_paid,2))