简体   繁体   English

如何在python中的for循环中更改变量?

[英]How do I change a variable in a for loop in python?

I am trying to make a program in python that calculates the remaining balance on a credit card. 我试图用python编写一个程序,计算信用卡上的余额。 It is for MIT open courseware "Introduction to Computer Science and Programming" . 它是针对MIT开放式课件“计算机科学与程序设计导论” I am doing problem set one . 我正在做第一个问题

The program has to ask the user for starting variables: starting balance, annual interest rate, and minimum monthly payment. 该程序必须询问用户初始变量:初始余额,年利率和每月最低还款额。 Here is my code. 这是我的代码。

initialOutstandingBalance= float(raw_input('What is the outstanding balance on your  
card?'))
annualInterestRate=float(raw_input('What is the annual interest rate expressed as a   
decimal?'))
minimumMonthlyPaymentRate=float(raw_input('What is the minimum monthly payment rate on
your card expressed as a decimal?'))

for month in range(1,13):
    print("Month: "+ str(month))
    minimumMonthlyPayment=float(minimumMonthlyPaymentRate*initialOutstandingBalance)
    interestPaid=float((annualInterestRate)/(12*initialOutstandingBalance))
    principalPaid=float(minimumMonthlyPayment-interestPaid)
    newBalance=float(initialOutstandingBalance-principalPaid)
    print("Minimum monthly payment: $"+str(minimumMonthlyPayment))
    print("Principle paid: $"+str(principalPaid))
    print("Remaining Balance: $"+str(newBalance))

How do I get the remaining balance to properly update? 如何获取剩余余额以正确更新? I can't figure out how to update the remaining balance at the end of each month. 我不知道如何在每个月底更新余额。 So far, each month returns the same values for minimum monthly payment, principal paid, and remaining balance. 到目前为止,每个月对于最低每月还款额,已付本金和剩余余额返回相同的值。

You're using the same initialOutstandingBalance variable throughout your loop, and never changing it. 您在整个循环中使用相同的initialOutstandingBalance变量,并且从不对其进行更改。 Instead, you should track the current balance. 相反,您应该跟踪当前余额。 That will be equal to the initial outstanding balance at the start of the loop, but will change as it runs. 这将等于循环开始时的初始未偿余额,但是会随着循环的进行而变化。

You also don't need to keep calling float . 您也不需要继续调用float

current_balance = initialOutstandingBalance
for month in range(1,13):
    print("Month: "+ str(month))
    minimumMonthlyPayment = minimumMonthlyPaymentRate * current_balance
    # this calculation is almost certainly wrong, but is preserved from your starting code
    interestPaid = annualInterestRate / (12*current_balance)
    principalPaid = minimumMonthlyPayment - interestPaid
    current_balance = current_balance - principalPaid
    print("Minimum monthly payment: $"+str(minimumMonthlyPayment))
    print("Principle paid: $"+str(principalPaid))
    print("Remaining Balance: $"+str(current_balance))

You want to keep the variable newBalance outside the loop, or else it'll be reassigned each iteration. 您希望将变量newBalance保留在循环之外,否则将在每次迭代时将其重新分配。 Also, you don't want to divide the interest rate by 12 times the balance, but instead divide it by 12 and then multiply the quotient by the balance. 同样,您也不想将利率除以余额的12倍,而是将其除以12,然后再将商乘以余额。 Finally, as noted above, you don't need all the float s. 最后,如上所述,您不需要所有的float

This should work: 这应该工作:

newBalance = initialOutstandingBalance

for month in range(1,13):
    print("Month: " + str(month))

    minimumMonthlyPayment = minimumMonthlyPaymentRate * newBalance
    interestPaid = annualInterestRate / 12 * newBalance
    principalPaid = minimumMonthlyPayment - interestPaid
    newBalance -= principalPaid

    print("Minimum monthly payment: $" + str(minimumMonthlyPayment))
    print("Principle paid: $" +  str(principalPaid))
    print("Remaining Balance: $" + str(newBalance))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM