#πŸ”’ Any improvements to this code?

7 messages Β· Page 1 of 1 (latest)

thorny oyster
#

I'm writing a script that calculates a table that produces monthly payment, interest amount, remaining balance, and payment due for each month after taking in an inital purchase price. The code works, but I was wondering if there was any improvements that could be made to this code just for my reference.

***purchasePrice = float(input("Please enter the purchase price: "))
downPayment = purchasePrice * .10
interestRate = 0.12
loanAmount = purchasePrice - downPayment
monthlyPayment = loanAmount * 0.05
currentBalance = loanAmount
month = 1

header = (f"{'Month':<6}{'Balance':<15}{'Interest':<15}{'Principal':<15}{'Payment':<15}{'Remaining Balance':<15}")

print(header)
while currentBalance > 0:
interestOwed = currentBalance * (interestRate / 12)
principalOwed = monthlyPayment - interestOwed

if principalOwed > currentBalance:
    principalOwed = currentBalance
    payment = principalOwed + interestOwed
    currentBalance = 0
else:
    payment = monthlyPayment
    currentBalance -= principalOwed

print(f"{month:<6}{loanAmount:<15.2f}{interestOwed:<15.2f}{principalOwed:<15.2f}{payment:<15.2f}{currentBalance:<15.2f}")

month +=1
loanAmount -= principalOwed

ruby grottoBOT
#

@thorny oyster

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

silk schooner
#

!code can you format your code like this so its more readable

ruby grottoBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

πŸ”’ Any improvements to this code?