Please note that both your examples do not run. They both need an extra
After correcting the error, your code works, but does not print two decimal places in all instances (output excerpt follows):
a. Python variable names are recommended to be all lower case (e.g.
b. Spaces are recommended before and after operators
c. A for loop is usually preferred to a while loop.
d. Rearranging and simplifying your balance equation
For future reference, Python coding rules (and recommendations) are in PEP-8. See https://www.python.org/dev/peps/pep-0008/
)
at the end of the print statement (line 9) to run.After correcting the error, your code works, but does not print two decimal places in all instances (output excerpt follows):
Output:Month 4 Remaining balance: 38.11
Month 5 Remaining balance: 37.2
Month 6 Remaining balance: 36.3
Month 7 Remaining balance: 35.43
Try the following new format line:balance = 42 annualInterestRate = .2 monthlyPaymentRate = 0.04 month = 1 while month <= 12: balance = (balance + balance * (annualInterestRate/12)) - (monthlyPaymentRate * (balance + balance * (annualInterestRate / 12))) print('Month {:2} Remaining balance: {:.2f}'.format(month, balance)) month += 1
Output:Month 1 Remaining balance: 40.99
Month 2 Remaining balance: 40.01
Month 3 Remaining balance: 39.05
Month 4 Remaining balance: 38.11
Month 5 Remaining balance: 37.20
Month 6 Remaining balance: 36.30
Month 7 Remaining balance: 35.43
Month 8 Remaining balance: 34.58
Month 9 Remaining balance: 33.75
Month 10 Remaining balance: 32.94
Month 11 Remaining balance: 32.15
Month 12 Remaining balance: 31.38
Now a couple of recommendations to improve your code and make it more pythonic:a. Python variable names are recommended to be all lower case (e.g.
annualInterestRate
should be annual_interest_rate
).b. Spaces are recommended before and after operators
month += 1
.c. A for loop is usually preferred to a while loop.
d. Rearranging and simplifying your balance equation
For future reference, Python coding rules (and recommendations) are in PEP-8. See https://www.python.org/dev/peps/pep-0008/
balance = 42 annualInterestRate = .2 monthlyPaymentRate = 0.04 #NOTE: range(1, 13) executes 1 thru 12 for month in range(1, 13): balance = (balance + balance * (annualInterestRate/12)) * (1.0 - monthlyPaymentRate) print('Month {:2} Remaining balance: {:.2f}'.format(month, balance))Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00