问题遇到的现象和发生背景
假设我要买一栋房子总价total_cost为1000000元,要求首付portion_down_payment比例为25%。目前我刚毕业,储蓄current_saving为0,通过合理投资我每年的利率回报为0.04,并且每个月都可以拿到利润回报加入到储蓄中。同时我预期入职后半年的工资上涨幅度semi_annual_raise为0.07,即半年后我每个月的工资比之前多7%。
要求询问我的年薪annual_salary,运用二分法得出每个月工资投入储蓄的比例saved_percent,使得我在三年后储蓄能够支付房子的首付,允许误差epsilon为100.
问题相关代码,请勿粘贴截图
semi_annual_raise = 0.07
annual_rate = 0.04
total_cost = 1000000
portion_down_payment = total_cost*0.25
annual_salary = float(input("enter your starting salary:"))
monthly_salary = annual_salary/12
saved_percent = 0
monthly_saving = monthly_salary*saved_percent
current_saving = 0
numMonth = 0
epsilon = 100
low = 0
high = 1
saved_percent = (low + high)/2
done = False
numGuesses = 0
while not done == True:
while numMonth <= 36:
current_saving = current_saving*(1+annual_rate/12) + monthly_saving
numMonth += 1
if numMonth % 6 == 0:
monthly_salary *= (1 + semi_annual_raise)
if abs(current_saving - portion_down_payment)>= epsilon:
if current_saving < portion_down_payment:
low = saved_percent
else:
high = saved_percent
saved_percent = (low + high)/2
numGuesses += 1
numMonth = 0
else :
done = True
print("Best saving rate is", saved_percent)
print("Steps in bisection search:", numGuesses)
运行结果及报错内容
中途无法继续运行
我的解答思路和尝试过的方法
我想要达到的结果
如果该代码思路可行,希望能够在这个基础上进行修改