Python Forum

Full Version: How to round this code without getting an error?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I first used the following code
balance=42
annualInterestRate=.2
monthlyPaymentRate=0.04


month=1
while month <=12:
    balance=(balance+balance*(annualInterestRate/12))-(monthlyPaymentRate*(balance+balance*(annualInterestRate/12)))
    print('Month ' +str(month) + ' Remaining balance: '+ str(balance)
    month+=1
I then realized I want the balance in line 9 to round to 2 decimal places so I edited my code to

balance=42
annualInterestRate=.2
monthlyPaymentRate=0.04


month=1
while month <=12:
    balance=(balance+balance*(annualInterestRate/12))-(monthlyPaymentRate*(balance+balance*(annualInterestRate/12)))
    print('Month ' +str(month) + ' Remaining balance: '+ str(round(balance, 2))
    month+=1
Note line 9, the only thing I changed was to print the balance as rounded.
Python is giving me an error when I try the second posted code, what is wrong with it??
Please post the full text of the error you are getting.
Please note that both your examples do not run. They both need an extra ) 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