开发者

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

This question/solution led me to another related question asked here - Help would be appreciated!

Updated current 开发者_Go百科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.


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


This is unrelated to any possible efficiency problems*, but you should look into the decimal module if you're doing financial arithmetic. Otherwise, you'll end up with strange rounding and representation errors.


*more precisely: It will reduce the efficiency but increase the correctness of your code.


You should use string interpolation or formatting instead of passing to str() and adding.

print 'Month: %d' % (month,)


For (speed) optimization in general you might want to read An Optimization Anecdote

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜