在for循环中计算运行总数 - Python [英] Calculate a running total during a for loop - Python

查看:1456
本文介绍了在for循环中计算运行总数 - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这个问题源于我以前的问题同时使用MIT的开放课件学习Python / CS。 - 在这里看到我以前的问题 -



我使用下面的代码来制作月付款和其他东西的清单。然而,在循环结束时,我需要给付已付款总额的总计。



原始代码

  balance = float(raw_input(未清余额:))
interestRate = float(raw_input(Interest Rate (1,12 + 1)中的月份:

minPayRate = float(raw_input(最低月度支付率:))
interestPaid = round (interestRate / 12.0 * balance,2)
minPayment = round(minPayRate * balance,2)
principalPaid = round(minPayment - interestPaid,2)
remainingBalance = round(balance - principalPaid,2 )


print'月:%d'%(month,)
print'最低每月付款:%.2f'%(minPayment,)
print'付款原则:%.2f'%(principalPaid,)
print'剩余余额:%.2f'%(remainingBalance,)

余额= remainingBalance

if月xrange(12,12 + 1):
print'RESULTS'
print'支付的总额:'
print'余额:%.2f'%(remainingBalance,)

问题是,我一直无法弄清楚如何保持付款总额。我尝试添加 totalPaid = round(interestPaid + principalPaid,2)但是这只是导致一个月的总和,我似乎无法得到它保持每个值月份,然后把它们全部加到最后打印出来。

另外我知道结果的金额应该是1131.12



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

固定代码

 余额= float(raw_input(Outstanding Balance:))
interestRate = float(raw_input(Interest Rate:))
minPayRate = float(raw_input(Minimum Monthly Payment Rate:))$ b $ (1,12 + 1)中的月总计= 0


interestPaid = round(interestRate / 12.0 * balance,2)
minPayment = round(minPayRate *余额,2)
principalpaid = round(minPayment - interestPaid,2)
remainingBalance = round(balance - principalPaid,2)
totalPaid + = round(minPayment,2)

print'月:%d'%(month,)
print'最低每月付款:%.2f'%(minPayment,)
print'付款原则:%.2f'%(principalPaid ,
print'剩余余额:%.2f'%(remainingBalance,)
$ b $余额= remainingBalance

如果xrange(12,12 + 1) :
打印'结果'
打印'支付的总金额:%。 2f'%(totalPaid,)
print'余额:%.2f'%(remainingBalance,)




  total_paid =h2_lin>解决方案

= 0

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

  total_paid + = 1 

total_paid = total_paid + 1 。您不希望在每次迭代中给 total_paid 一个新的值,而是希望添加到它的现有值中。



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


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,)

解决方案

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_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.

这篇关于在for循环中计算运行总数 - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆