在 for 循环期间计算运行总数 - Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4997859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate a running total during a for loop - Python
提问by tehaaron
Edit: Below is my working code based on the feedback/answers I recieved.
编辑:以下是我根据收到的反馈/答案编写的工作代码。
This question stems from my previous question that came up while learning Python/CS using open courseware from MIT. --See my previous question here--
这个问题源于我之前在使用 MIT 的开放课件学习 Python/CS 时提出的问题。--请在此处查看我之前的问题--
I am using the following code to make a list of month payments and other things. However at the end of the loop I need to give a running total for the total amount that has been paid of the months.
我正在使用以下代码来列出月付款和其他事项。但是,在循环结束时,我需要给出几个月已支付的总金额的运行总计。
Original 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 month in xrange(12, 12+1):
print 'RESULTS'
print 'Total amount paid: '
print 'Remaining balance: %.2f' % (remainingBalance,)
The problem is that I have not been able to figure out how to keep a running total of the amounts paid. I tried adding totalPaid = round(interestPaid + principalPaid, 2)but that just led to a total for a single month, I cant seem to get it to keep that value for each month and then add them all up at the end to be printed out.
问题是我一直无法弄清楚如何保持已支付金额的总和。我尝试添加,totalPaid = round(interestPaid + principalPaid, 2)但这只是导致一个月的总数,我似乎无法让它保持每个月的值,然后在最后将它们全部加起来以打印出来。
Also I know that the resulting amount should be 1131.12
我也知道结果金额应该是 1131.12
I have found many examples of doing this when each value is know, via a list, but I cant seem to extrapolate that correctly.
通过列表,我发现了许多在知道每个值时执行此操作的示例,但我似乎无法正确推断。
Fixed Code
固定码
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,)
采纳答案by Rafe Kettler
Before your loop, initialize a variable to accumulate value:
在循环之前,初始化一个变量以累积值:
total_paid = 0
And then, in the body of your loop, add the appropriate amount to it. You can use the +=operator to add to an existing variable, e.g.
然后,在循环体中,添加适当的数量。您可以使用+=运算符添加到现有变量,例如
total_paid += 1
is a short form for total_paid = total_paid + 1. You don't want to give total_paida new value each iteration, rather you want to add to its existing value.
是 的缩写形式total_paid = total_paid + 1。您不希望total_paid每次迭代都赋予新值,而是希望添加到其现有值中。
I'm not sure about the specifics of your problem, but this is the general form for accumulating a value as you loop.
我不确定您的问题的具体情况,但这是在循环时累积值的一般形式。
回答by erKURITA
You actually have to initialize totalPaid to 0 and then
您实际上必须将 totalPaid 初始化为 0 然后
totalPaid = round(interestPaid + principalPaid, 2) + totalPaid
Inside the loop. Your problem is that you're not accumulating the total, you're just setting a new one on each iteration.
圈内。你的问题是你没有累积总数,你只是在每次迭代中设置一个新的。
回答by GSto
sounds like you were close. The problem is that you were overwriting the total each time. Try something like this:
听起来你很接近。问题是你每次都覆盖总数。尝试这样的事情:
totalPaid = totalPaid + round(interestPaid + principalPaid, 2)
回答by Fred Nurk
You always make the minimum payment? Just use minPayment instead of figuring out that math again. Keep a running total, then print it out afterthe loop.
你总是支付最低还款额?只需使用 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,)
Also notice that range has exactly one value, so you'd just check month == 12, but it's simply not necessary here.
另请注意,范围只有一个值,因此您只需检查月 == 12,但在这里根本没有必要。

