【问题标题】:Python Beginner. Is this Python code as efficient as it could be?Python初学者。这个 Python 代码是否尽可能高效?
【发布时间】:2011-02-14 08:38:52
【问题描述】:

这个问题/解决方案让我提出了另一个相关问题here - 非常感谢您的帮助!

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

我是 Python 的新手(这是我的第二个程序)。我目前正在使用麻省理工学院的开放课件来介绍使用 Python Academic Earth videos 的 CS,并且我正在研究问题集 1 Viewable Here。我已经创建了这个程序,该程序在 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

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

非常感谢帮助我走到今天这一步的人。

【问题讨论】:

标签: python


【解决方案1】:
print "x: " + str(x)

应替换为:

print "x:", x

这是打印的特殊情况。


将循环改为:

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

删除第一个循环的检查,只需将 balance 设置为 remainingBalance 作为结束。

因为您手动增加月份,所以每次都打印错误的值。


如果你的意思是效率和执行效率,你担心的是too soon。如果您的意思是复制代码,那么您确实在循环之前不必要地复制了数学。结合以上:

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

【讨论】:

  • 非常有帮助!我确实注意到我的计算有一个月的时间,所以我将if month &gt; 1 更改为if month &gt;= 1 并修复了它,但我更喜欢你使用for 的方法,就像上面提到的Ignacio。另外,我关于效率的问题是关于额外的、不必要的(重复的)代码。当我尝试删除顶部的数学时,我的原始代码不起作用,但现在使用for 循环是可能的。非常感谢你。最后,我最初使用了print "x:", x 代码,但我注意到它出于某种原因添加了一个额外的空间。
  • 也许我错了,现在第一个月后什么都没有改变。
  • @tehaaron:听起来你错过了我添加到循环体底部的那行;它分配给平衡。
  • 绝对是一个拼写错误……读了 10 遍。非常感谢。
  • 我在这里添加了一个后续问题:stackoverflow.com/questions/4997859/… 如果您愿意参与。再次感谢。
【解决方案2】:

这与任何可能的效率问题无关*,但如果您正在做金融算术,您应该查看decimal 模块。否则,你会得到奇怪的rounding and representation errors


*更准确地说:它会降低效率,但会增加代码的正确性。

【讨论】:

  • 我确实研究过并应用了它,但为了获得与问题集完全相同的结果,我将其改回。
  • 显然,那些麻省理工学院的人认为他们很聪明:O
【解决方案3】:

您应该使用string interpolationformatting 而不是传递给str() 并添加。

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

【讨论】:

  • 谢谢,我对我的代码进行了更改。我用来重复计算的方法做得好/正确吗?
  • 当我使用%f 格式化浮点数时,有没有办法将小数限制为 2?
  • 绝对。如文档中所述,请在格式说明符中给出精度。 &gt;&gt;&gt; '%.2f' % 123.456'123.46'
  • 通常for 循环用于循环已知次数,但在特定迭代中执行某些操作可能会很棘手(但在这种情况下并非如此)。另外,请参阅enumerate() 以供将来参考。
  • 啊,我问这个问题太快了。我查了一下,当我回来删除/回答自己时,你已经打败了我!谢谢! (参考小数位)
【解决方案4】:

对于一般的(速度)优化,您可能需要阅读An Optimization Anecdote

【讨论】:

    猜你喜欢
    • 2021-10-23
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多