for循环里python计算程序跑的时间_在for循环中计算运行总数 - Python

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值