Horuson 2022-07-16 21:51 采纳率: 100%
浏览 32
已结题

python中二分法与控制流练习

问题遇到的现象和发生背景

假设我要买一栋房子总价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)

运行结果及报错内容

中途无法继续运行

我的解答思路和尝试过的方法
我想要达到的结果

如果该代码思路可行,希望能够在这个基础上进行修改

  • 写回答

1条回答 默认 最新

  • 请叫我问哥 Python领域新星创作者 2022-07-17 02:45
    关注

    修改如下:

    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
    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:
            monthly_saving = monthly_salary*saved_percent
            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
            monthly_salary = annual_salary/12
            current_saving = 0
            numGuesses += 1
            numMonth = 0
        else :
            done = True
        
    print("Best saving rate is", saved_percent)
    print("Steps in bisection search:", numGuesses)
    

    试运行结果:

    enter your starting salary:120000
    Best saving rate is 0.531982421875
    Steps in bisection search: 11
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 7月25日
  • 已采纳回答 7月17日
  • 修改了问题 7月16日
  • 创建了问题 7月16日

悬赏问题

  • ¥15 单片机配网,继电器开关,广播
  • ¥60 Qcustomplot绘制实时动态曲线
  • ¥20 运用matlab画x-y图
  • ¥15 用idea运行项目,运行tomcat报错:断言失败
  • ¥15 Sqlserver查询链接服务器数据问题
  • ¥15 Bibtex4Word 引用中文文献
  • ¥20 用opencv c/c++ 转换成灰度图,然后做一下直方图均衡,输出mp4文件
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题
  • ¥30 gradle环境下javafx项目如何使用druid连接池