Beginner Python. Is this Python code as efficient as possible?

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

Updated current code below based on original feedback

I am completely new to Python (this is my second program). I am currently using Open Courseware from MIT to get an introduction to CS using the Python Academic Earth videos , and I am working on a set of problems 1 view here . I created this program that successfully recreates "test case 1" after 12 months (excluding the "results" section ... is still working on it), but my question is, is the following (my) code as efficient as possible? I feel like I am repeating it when it may not be needed.

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 figure this out.

+3
source share
4 answers
print "x: " + str(x)

Should be replaced by:

print "x:", x

.


:

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

.

, .


, , . , . :

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
+4

*, decimal, . .


* : , .

+4

str() .

print 'Month: %d' % (month,)
+3
source

For optimization (speed) in general, you can read the Optimization Joke

+1
source

Source: https://habr.com/ru/post/1791941/


All Articles