Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Beginner. Is this Python code as efficient as it could be?

Tags:

python

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.

like image 565
tehaaron Avatar asked Feb 17 '26 18:02

tehaaron


1 Answers

print "x: " + str(x)

Should be replaced by:

print "x:", x

This is a special case with print.


Change the looping to:

for month in xrange(1, 12+1):

Drop the check for the first loop and simply set balance to remainingBalance as the end.

Because you increment month manually, you print the wrong value each time.


If you mean efficiency as in execution efficiency, you're worrying about that too soon. If you mean it as in duplicating code, you do needlessly duplicate the math before the loop. Combined with the above:

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:', month
  print 'Minimum monthly payment:', minPayment
  print 'Principle paid:', principalPaid
  print 'Remaining balance:', remainingBalance

  balance = remainingBalance
like image 144
Fred Nurk Avatar answered Feb 19 '26 06:02

Fred Nurk