This question/solution led me to another related question asked here - Help would be appreciated!
Updated current code below based on initial feedback
I am brand new to Python (this is my second program). I am currently using the Open Courseware from MIT to get an intro to CS using Python Academic Earth videos and I am working on Problem Set 1 Viewable Here. I have created this program that successfully recreates "Test Case 1" through the 12 months (excluding the "results" section...still working on that) but my question is, is the following (my) code as efficient as possible? I feel like I am repeating myself in it when it may not be necessary. :
Original Code:
balance = float(raw_input("Outstanding Balance: "))
interestRate = float(raw_input("Interest Rate: "))
minPayRate = float(raw_input("Minimum Monthly Payment Rate: "))
interestPaid = round((interestRate/12.0)*balance, 2)
minPayment = round(minPayRate*balance, 2)
principalPaid = round(minPayment-interestPaid, 2)
remainingBalance = round(balance-principalPaid, 2)
month = 1
while month < 12 :
if month > 1 :
balance = remainingBalance
interestPaid = round((interestRate/12.0)*balance, 2)
minPayment = round(minPayRate*balance, 2)
principalPaid = round(minPayment-interestPaid, 2)
remainingBalance = round(balance-principalPaid , 2)
month = month+1
print 'Month: ' + str(month)
print 'Minimum monthly payment: ' + str(minPayment)
print 'Principle paid: ' + str(principalPaid)
print 'Remaining balance: ' + str(remainingBalance)
Current Code
balance = float(raw_input("Outstanding Balance: "))
interestRate = float(raw_input("Interest Rate: "))
minPayRate = float(raw_input("Minimum Monthly Payment Rate: "))
for month in xrange(1, 12+1):
interestPaid = round(interestRate / 12.0 * balance, 2)
minPayment = round(minPayRate * balance, 2)
principalPaid = round(minPayment - interestPaid, 2)
remainingBalance = round(balance - principalPaid, 2)
print 'Month: %d' % (month,)
print 'Minimum monthly payment: %.2f' % (minPayment,)
print 'Principle paid: %.2f' % (principalPaid,)
print 'Remaining balance: %.2f' % (remainingBalance,)
balance = remainingBalance
If you see anything else in this new code let me know!
Many thanks to those who helped me get it this far.