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 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库
  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?