Python 初学者。这段 Python 代码是否尽可能高效?

2024-01-29

这个问题/解决方案让我想到了另一个相关问题here https://stackoverflow.com/questions/4997859/calculate-a-running-total-during-a-for-loop-python- 帮助将不胜感激!

根据初始反馈更新了下面的当前代码

我对 Python 是全新的(这是我的第二个程序)。我目前正在使用 MIT 的开放课件来了解使用 Python 进行 CS 的介绍学术地球视频 http://www.academicearth.org/courses/introduction-to-computer-science-and-programming我正在研究问题集 1可以在这里查看 http://mit600.mit.edu/blog/problem-sets/problem-set-1/。我已经创建了这个程序,该程序在 12 个月内成功地重新创建了“测试用例 1”(不包括“结果”部分......仍在研究中),但我的问题是,以下(我的)代码是否尽可能高效?我觉得我在重复自己,而这可能没有必要。 :

原始代码:

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)

当前代码

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

如果您在这个新代码中看到任何其他内容,请告诉我!

非常感谢那些帮助我走到这一步的人。


print "x: " + str(x)

应替换为:

print "x:", x

这是带有打印的特殊情况。


将循环更改为:

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

删除第一个循环的检查,并将余额设置为remainingBalance 作为结束。

由于您手动增加月份,因此每次都会打印错误的值。


如果您指的是执行效率中的效率,那么您会担心这一点too soon http://tinyurl.com/knuth-premature。如果您的意思是复制代码,那么您就不必要地在循环之前复制数学。结合以上:

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 初学者。这段 Python 代码是否尽可能高效? 的相关文章

随机推荐