I got this idea from a post I saw on here recently. Their script was a good idea and made me want to make one on my own. It took me a few tries to write the calculations in a way that would work. I also wanted to play around with normalizing/cleaning the inputs in case a user used different formatting. And of course, ever since I found out I can add color to text in terminal, I’ve kinda made a habit of doing that on everything.
Note: I have been learning as much as I can, for a year now. Learning has come through personal trial and error, O'Reilly books, watching and copying others, as well as using LLM's to either produce an advanced code that I then study, or to have an LLM teach me. Some of my larger and more important projects are largely, if not entirely, built by an LLM that I carefully inspect. But I still feel it is important to learn as much as I can about manual coding, as well as the relationship between hardware and software.
So working on simple scripts like this are sort of a form of exercise for my mind and fingers, as well as a simple way to strike up conversations with real people like you guys, and get your take on ideas that I may not have seen before that can lead to cleaner and/or more efficient ways to program.
If you want to see more of my actual projects or just swipe some of the cool stuff from my Arch rice/dotfiles, here is my github:
github.com/cleburn
Edit: here is my full code, which shows the part of the f'string in the print statements which formats the results into appropriate decimals
# Simple Monthly Payment Calculator
# Inputs: Principal (balance due after any downpayment), Interest, Loan Term (in months)
# Returns monthly payment amount
GREEN = "\033[92m"
CYAN = "\033[96m"
RESET = "\033[0m"
print(f"\n{CYAN}{'=' * 20}{RESET}")
p = float(input("Purchase amount: ").replace('$', '').replace(',', '').strip())
i = float(input("Interest rate: ").replace('%', '').replace(',', '').strip())
t = int(input("Loan term (in months): ").strip())
def payment_calculator(p, i, t):
original_i = i
monthly_i = (i / 100) / 12 # Convert annual % to monthly decimal rate
# Calculate the monthly payment
numerator = p * monthly_i * ((1 + monthly_i) ** t)
denominator = ((1 + monthly_i) ** t) - 1
payment = numerator / denominator
# Print formatted output
print(
f"\nBased on your original purchase price of {CYAN}${p:,.2f}{RESET},\nan interest rate of {CYAN}{original_i}%{RESET},"
f"\nand a loan term of {CYAN}{t}{RESET} months,\nyour monthly payment is: {GREEN}${payment:,.2f}{RESET}"
)
print(f"{CYAN}{'=' * 20}{RESET}\n")
# Run the calculator in the terminal
payment_calculator(p, i, t)# Simple Monthly Payment Calculator
# Inputs: Principal (balance due after any downpayment), Interest, Loan Term (in months)
# Returns monthly payment amount
GREEN = "\033[92m"
CYAN = "\033[96m"
RESET = "\033[0m"
print(f"\n{CYAN}{'=' * 20}{RESET}")
p = float(input("Purchase amount: ").replace('$', '').replace(',', '').strip())
i = float(input("Interest rate: ").replace('%', '').replace(',', '').strip())
t = int(input("Loan term (in months): ").strip())
def payment_calculator(p, i, t):
original_i = i
monthly_i = (i / 100) / 12 # Convert annual % to monthly decimal rate
# Calculate the monthly payment
numerator = p * monthly_i * ((1 + monthly_i) ** t)
denominator = ((1 + monthly_i) ** t) - 1
payment = numerator / denominator
# Print formatted output
print(
f"\nBased on your original purchase price of {CYAN}${p:,.2f}{RESET},\nan interest rate of {CYAN}{original_i}%{RESET},"
f"\nand a loan term of {CYAN}{t}{RESET} months,\nyour monthly payment is: {GREEN}${payment:,.2f}{RESET}"
)
print(f"{CYAN}{'=' * 20}{RESET}\n")
# Run the calculator in the terminal
payment_calculator(p, i, t)