0

我试图找到一个储蓄率(rate_approx在我的代码中),这将导致current_savings超过 36 个月的储蓄(在代码中)在 100 美元的首付(down_payment在代码中)以内。

for每次在循环内运行while循环以表示current_savings36 个月期间的增长。然后,一旦根据它是否太高或太低来调整储蓄率,我希望它保存一个新的savings_rate并在 36 个月期间尝试。所以我重置current_savings以确保它不是从current_savings之前的for循环运行开始,然后再试一次。

house_price = float(1000000)
down_payment = float(house_price*0.25)
high = float(1)
low = float(0)
rate_approx = float((low + high)/2)
salary = float(150000)
monthly_salary = float(salary/12)
monthly_contribution = float(monthly_salary*rate_approx)
semi_annual_raise = float(1.07)
APR = float(1.04)
current_savings = 0

while abs(current_savings - down_payment) > 100:
    current_savings = 0
    for n in range(35):
        current_savings = float((current_savings + monthly_contribution) * APR)
        if n > 0 and n%6 == 0:
            monthly_salary = float(monthly_salary * semi_annual_raise)
            monthly_contribution = float(monthly_salary*rate_approx)

  if current_savings < down_payment + 100:
    current_savings = 0
    low = rate_approx
    rate_approx = (low + high)/2
    print("low")
    print("savings rate:",rate_approx)

  elif current_savings > down_payment + 100:
    current_savings = 0
    high = rate_approx
    rate_approx = (low + high)/2
    print("high")
    print("savings rate:",rate_approx)

  else:
    break
print("savings rate:", float(rate_approx))
print("savings:", float(current_savings))

为什么程序会继续输出越来越小的储蓄率,而最终current_savings价值却越来越大?

4

0 回答 0