如何在循环中更改条件,但仅在第一次迭代后更改?

时间:2017-06-16 21:27:31

标签: python loops for-loop while-loop

我创建了一个简单的模型,用于计算在12个月内偿还信用卡余额所需的最低固定月付款额。

我的代码:

balance = 3329             # starting balance
annualInterestRate = 0.2   # yearly interest rate
minFixedPayment = 0        # initiate a minimum monthly payment of 0

while balance > 0:
    minFixedPayment = minFixedPayment + 10
    for i in range(1,13,1):
        unpaidBalance = balance - minFixedPayment
        balance = unpaidBalance + ((annualInterestRate/12) * unpaidBalance)
        i=i+1
print(round(minFixedPayment,2))

我添加了" minFixedPayment"在for循环中增加BEORE,使其保持递增,直到它找到一个值,其中12月末12的余额为< = 0.我知道这是错误的,因为它将使用minFixedPayment = 10计算第一次迭代,而不是0。有更好的方法来安排这个吗?

我尝试添加

minFixedPayment = minFixedPayment + 10

语句到循环结束,如下所示:

while balance > 0:
    for i in range(1,13,1):
        unpaidBalance = balance - minFixedPayment
        balance = unpaidBalance + ((annualInterestRate/12) * unpaidBalance)
        i=i+1
    minFixedPayment = minFixedPayment + 10 # moved this to after the loop
print(round(minFixedPayment,2))

但这会使while循环永远运行(为什么?)。

有没有更好的方法来解决这个问题? 谢谢你的时间。

3 个答案:

答案 0 :(得分:1)

一些问题:

  • 由于您的目的是尝试使用不同的付款金额,因此您应该在每次尝试时将余额重置为原始值。没有它,你可以进入一个不断增加的平衡的无限循环。为了能够重置余额,您还需要以第二个名称存储它。
  • 您不应该使用i增加i=i+1。已经使用for循环来处理这个问题。

以下是建议的代码:

loan = balance = 3329      # starting balance, use two names so you can restart
annualInterestRate = 0.2   # yearly interest rate
minFixedPayment = -10      # initiate a minimum monthly payment

monthlyInterestRate = annualInterestRate/12 # avoid doing this repeatedly

while balance > 0:
    minFixedPayment = minFixedPayment + 10
    balance = loan # start from scratch
    for i in range(1,13):
        unpaidBalance = balance - minFixedPayment
        balance = unpaidBalance + monthlyInterestRate * unpaidBalance

print(round(minFixedPayment,2))

答案 1 :(得分:1)

为了您的兴趣,这里有一个更复杂的求解器:

from functools import partial
from math import ceil

def diff(fn, x, h=0.001):
    """
    Numerically differentiate fn at x
    """
    return (fn(x + h) - fn(x)) / h

def newton_solver(fn, target_y, initial_x, max_reps=100, max_err=0.01):
    """
    Find a value for x such that fn(x) == target_y (+/- max_err)
    """
    x = initial_x
    for _ in range(max_reps):
        err = fn(x) - target_y
        if abs(err) <= max_err:
            # found a good enough solution
            return x
        else:
            # first-order correction to reduce error
            x -= err / diff(fn, x)
    raise ValueError("solver failed to converge")

def final_balance(fixed_payment, initial_balance, period_rate, num_periods):
    """
    Calculate the final balance on a fixed payment plan
    """
    balance = initial_balance
    for _ in range(num_periods):
        balance = (balance - fixed_payment) * (1. + period_rate)
    return balance

def round_up_to_next_cent(amt):
    return ceil(amt * 100.) / 100.

def main():
    initial_balance = 3329.
    annual_interest = 0.2

    # bind arguments to create a single-argument function to pass to newton_solver
    my_final_balance = partial(final_balance, initial_balance = initial_balance, period_rate = annual_interest / 12, num_periods = 12)

    # initial guess - in total you will pay about half a year's interest
    monthly_payment_guess = initial_balance * (1. + annual_interest * 0.5) / 12

    # solve to find accurate value
    monthly_payment = newton_solver(my_final_balance, 0., monthly_payment_guess)
    monthly_payment = round_up_to_next_cent(monthly_payment)

    # and report the result
    print("A fixed monthly payment of ${:0.2f} results in a final balance of ${:0.2f}".format(monthly_payment, my_final_balance(monthly_payment)))

if __name__ == "__main__":
    main()

产生

A fixed monthly payment of $303.33 results in a final balance of $-0.07

答案 2 :(得分:0)

添加一个标志变量来控制是否更改余额。另一种方法是检查余额是否改变。 (if balance != original_balance: ...) 例如

balance = 3329             # starting balance
annualInterestRate = 0.2   # yearly interest rate
minFixedPayment = 0        # initiate a minimum monthly payment of 0
change_flag = 0
while balance > 0:
    if change_flag: minFixedPayment = minFixedPayment + 10
    change_flag = 1
    for i in range(1,13,1):
        unpaidBalance = balance - minFixedPayment
        balance = unpaidBalance + ((annualInterestRate/12) * unpaidBalance)
        i=i+1
print(round(minFixedPayment,2))