计算 for 循环期间的运行总计 - Python

发布于 2024-10-17 12:42:21 字数 2339 浏览 6 评论 0原文

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

这个问题源于我之前使用麻省理工学院的开放课件学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

只为一人 2024-10-24 12:42:21

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

total_paid = 0

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

total_paid += 1

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

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

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.

眸中客 2024-10-24 12:42:21

你总是支付最低还款额吗?只需使用 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,)

另请注意,范围只有一个值,因此您只需检查 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.

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.

始终不够 2024-10-24 12:42:21

这个答案对我有用:

首先,创建派生值:

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']
Index_DateABCD
2015/01/3110101010
2015/02/01232322
2015/02/021060290280

This answer worked for me:

First, create the derived value:

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

Then iterate through the remaining rows and fill the calculated values:

for i in range(1, len(df)):
    df.loc[i, 'C'] = df.loc[i-1, 'C'] * df.loc[i, 'A'] + df.loc[i, 'B']
Index_DateABCD
2015/01/3110101010
2015/02/01232322
2015/02/021060290280
苍暮颜 2024-10-24 12:42:21

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

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

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

You actually have to initialize totalPaid to 0 and then

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.

你另情深 2024-10-24 12:42:21

听起来你很接近。问题是你每次都会覆盖总数。尝试这样的事情:

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文