1

编辑:下面是我根据收到的反馈/答案的工作代码。

这个问题源于我之前在使用 MIT 的开放课件学习 Python/CS 时提出的问题。--在这里查看我之前的问题--

我正在使用以下代码列出每月付款和其他内容。但是,在循环结束时,我需要给出月份支付的总金额的运行总计。

原始代码

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 month in xrange(12, 12+1):
        print 'RESULTS'
        print 'Total amount paid: '
        print 'Remaining balance: %.2f' % (remainingBalance,)

问题是我无法弄清楚如何保持支付金额的总和。我尝试添加totalPaid = round(interestPaid + principalPaid, 2),但这只会导致一个月的总数,我似乎无法让它保持每个月的值,然后在最后将它们全部加起来以打印出来。

我也知道结果量应该是 1131.12

当通过列表知道每个值时,我发现了很多这样做的例子,但我似乎无法正确推断。

固定代码

balance = float(raw_input("Outstanding Balance: "))
interestRate = float(raw_input("Interest Rate: "))
minPayRate = float(raw_input("Minimum Monthly Payment Rate: "))
totalPaid = 0

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)
    totalPaid += round(minPayment, 2)

    print 'Month: %d' % (month,)
    print 'Minimum monthly payment: %.2f' % (minPayment,)
    print 'Principle paid: %.2f' % (principalPaid,)
    print 'Remaining balance: %.2f' % (remainingBalance,)

    balance = remainingBalance

    if month in xrange(12, 12+1):
        print 'RESULTS'
        print 'Total amount paid: %.2f' % (totalPaid,)
        print 'Remaining balance: %.2f' % (remainingBalance,)
4

5 回答 5

3

在循环之前,初始化一个变量以累积值:

total_paid = 0

然后,在循环体中,添加适当的量。您可以使用+=运算符添加到现有变量,例如

total_paid += 1

是 的缩写形式total_paid = total_paid + 1。您不想total_paid每次迭代都赋予新值,而是想添加到其现有值。

我不确定您的问题的具体情况,但这是在循环时累积值的一般形式。

于 2011-02-14T22:13:36.633 回答
1

您总是支付最低付款吗?只需使用 minPayment 而不是再次计算该数学。保持一个运行总数,然后在循环打印出来。

balance = float(raw_input("Outstanding Balance: "))
interestRate = float(raw_input("Interest Rate: "))
minPayRate = float(raw_input("Minimum Monthly Payment Rate: "))
paid = 0

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)
    paid += minPayment

    print  # Make the output easier to read.
    print 'Month: %d' % (month,)
    print 'Minimum monthly payment: %.2f' % (minPayment,)
    print 'Principle paid: %.2f' % (principalPaid,)
    print 'Remaining balance: %.2f' % (remainingBalance,)

    balance = remainingBalance

print
print 'RESULTS'
print 'Total amount paid:', paid
print 'Remaining balance: %.2f' % (remainingBalance,)

另请注意,范围只有一个值,因此您只需检查月份 == 12,但这里根本没有必要。

于 2011-02-14T22:18:19.000 回答
1

这个答案对我有用:

首先,创建派生值:

df.loc[0, 'C'] = df.loc[0, 'D']

然后遍历剩余的行并填充计算值:

for i in range(1, len(df)):
    df.loc[i, 'C'] = df.loc[i-1, 'C'] * df.loc[i, 'A'] + df.loc[i, 'B']
索引_日期 一种 C D
2015/01/31 10 10 10 10
2015/02/01 2 3 23 22
2015/02/02 10 60 290 280
于 2021-05-26T10:14:52.240 回答
0

您实际上必须将 totalPaid 初始化为 0 然后

totalPaid = round(interestPaid + principalPaid, 2) + totalPaid

循环内。你的问题是你没有累积总数,你只是在每次迭代中设置一个新的。

于 2011-02-14T22:16:29.647 回答
0

听起来你很亲密。问题是您每次都覆盖了总数。尝试这样的事情:

totalPaid = totalPaid + round(interestPaid + principalPaid, 2)
于 2011-02-14T22:18:05.007 回答