Python Forum
How to round this code without getting an error?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to round this code without getting an error?
#1
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??
Reply
#2
Please post the full text of the error you are getting.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Round off floats in a list nagymusic 10 4,811 Apr-02-2019, 12:43 AM
Last Post: nagymusic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020