编辑:以下是根据我收到的反馈/答案的工作代码。
这个问题源于我之前在使用麻省理工学院开放课件学习Python / CS时提出的问题。 - See my previous question here -
我使用以下代码列出月付款和其他内容。但是,在循环结束时,我需要给出已经支付的总金额的累计金额。
原始代码
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,)
答案 0 :(得分:2)
在循环之前,初始化变量以累积值:
total_paid = 0
然后,在循环体中,添加适当的量。您可以使用+=
运算符添加到现有变量,例如
total_paid += 1
是total_paid = total_paid + 1
的缩写。您不希望每次迭代都为total_paid
赋予一个新值,而是希望添加到其现有值。
我不确定问题的具体细节,但这是循环时累积值的一般形式。
答案 1 :(得分: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,但这里根本没有必要。
答案 2 :(得分:0)
您实际上必须将totalPaid初始化为0然后
totalPaid = round(interestPaid + principalPaid, 2) + totalPaid
循环内部。你的问题是你没有累积总数,你只是在每次迭代时设置一个新的。
答案 3 :(得分:0)
totalPaid = totalPaid + round(interestPaid + principalPaid, 2)
答案 4 :(得分:0)
这 answer 对我有用:
首先创建派生值:
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']
索引日期 | A | B | C | D |
---|---|---|---|---|
2015/01/31 | 10 | 10 | 10 | 10 |
2015/02/01 | 2 | 3 | 23 | 22 |
2015/02/02 | 10 | 60 | 290 | 280 |