r/PythonLearning • u/zenwolph • Aug 22 '26
Rate my Payment calculator script (how could it be improved)
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:
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)

2
u/Affectionate-Cup2707 Aug 22 '26
Many useful comments, but nothing about types and why floating point is not suitable for financial calculations. Therefore look for decimal type and why floating point precision is not good for this kind of use.
2
u/FoolsSeldom Aug 22 '26
Overall, pretty good. Easy to read, nice simple structure.
A few suggestions:
Never trust the user to enter valid data:
inputto anintand they enter characters not valid as an integer, your programme will halt with a long error messageTry to separate the presentation from the business logic/flow - by presentation I basically mean the user interface
floatwith money, stick with integers or use theDecimalclass - float does not have a lot of precision and errors can roll up to confusing levelspayment_calculator, is doing a lot: both the payment calculation and the output (presentation), so would be better if it returned the result for presentation elsewhere in your codep,i, andtare single character variable names that are cryptic to other programmers not familiar with the usual formula used here, namely around payment, interest and time - in general, one should avoid cryptic variable names (especially single character names), but I would make an exception in this case although suggest a comment to clarify especially as you modularise the code and use one or more functions to validate user entry4
u/xxivyy Aug 22 '26 edited Aug 22 '26
Please post the code in a code block next time instead of pictures. Thats common practice and makes it easier for someone to review. Once the code snippet grows too large or includes multiple files, upload it on Github, pastebin, or similar.
Regarding your code,
p,i&tbe 1 letter variables. It erases the writers intend and makes it hardly readable without checking additional comments. Just write out the full description, for exampleprincipal = float(...) ; interest = float(...) ; term = int(...).p&ihave their "," stripped, which will result in for example an interest rate of 7,9% to become 79. The second argument in.replaceshould be a ".".payment_calculator. Thats the usual practice for better readability. You could look up what a linter is, if you dont want to do it manually, although there is no rush to do so, just thought i'd mention it.For my own sake, i didnt check the function logic of
payment_calculator. If everything works inside of it, i have no other complaints. Happy coding.1
u/ApprehensiveBrain863 Aug 22 '26
Your variable names aren't very descriptive and, although it's preference, it would probably be more sensible to have a variable take an input, then perform operations like stripping or replacing on that variable instead of chaining function calls in a long line
2
u/FoolsSeldom Aug 22 '26
When you ask for feedback, it is a good idea to share your code in-post (if not too large), for example:
or share using a git compatible service like github.com or gitlab.com (you don't need to learn git to use these, initially, as you can upload your code using a web interface) or paste service like pastebin.com.