计算 for 循环期间的运行总计 - Python
编辑:下面是我的工作代码,基于我收到的反馈/答案。
这个问题源于我之前使用麻省理工学院的开放课件学习 Python/CS 时提出的问题。 --在这里查看我之前的问题< /a>--
我正在使用以下代码来列出每月付款和其他事项的列表。然而,在循环结束时,我需要给出月份已支付总额的运行总计。
原始代码
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,)
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--
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.
Also I know that the resulting amount should be 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,)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在循环之前,初始化一个变量来累加值:
然后,在循环体中,向其添加适当的金额。您可以使用
+=
运算符添加到现有变量,例如total_paid =total_paid + 1
的缩写形式。您不想每次迭代都为total_paid
提供新值,而是希望添加到其现有值。我不确定你的问题的具体情况,但这是循环时累积值的一般形式。
Before your loop, initialize a variable to accumulate value:
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.is a short form for
total_paid = total_paid + 1
. You don't want to givetotal_paid
a new value each iteration, rather you want to add to its existing value.I'm not sure about the specifics of your problem, but this is the general form for accumulating a value as you loop.
你总是支付最低还款额吗?只需使用 minPayment 而不是再次计算数学。保留运行总计,然后在循环后将其打印出来。
另请注意,范围只有一个值,因此您只需检查 Month == 12,但这里根本没有必要。
You always make the minimum payment? Just use minPayment instead of figuring out that math again. Keep a running total, then print it out after the loop.
Also notice that range has exactly one value, so you'd just check month == 12, but it's simply not necessary here.
这个答案对我有用:
首先,创建派生值:
然后迭代剩余的行并填充计算值:
This answer worked for me:
First, create the derived value:
Then iterate through the remaining rows and fill the calculated values:
您实际上必须将totalPaid初始化为0,然后
在循环内。你的问题是你没有累积总数,你只是在每次迭代中设置一个新的。
You actually have to initialize totalPaid to 0 and then
Inside the loop. Your problem is that you're not accumulating the total, you're just setting a new one on each iteration.
听起来你很接近。问题是你每次都会覆盖总数。尝试这样的事情:
sounds like you were close. The problem is that you were overwriting the total each time. Try something like this: