python 在MIT 6.0001习题集1部分C的代码中找不到我的错误

ehxuflar  于 2023-01-11  发布在  Python
关注(0)|答案(1)|浏览(241)

我是新的python,我将非常感谢,如果有人能帮我找到我的代码中的错误。谢谢你提前。
这个问题要求我们找到一个最佳储蓄率,以在36个月内支付一套房子的首付款。由于准确地达到这个目标是一个挑战,答案只需要在所需首付款的100美元以内。
为了简化问题,假设:
1.你的半年加薪是0.07(7%)
1.您的投资年回报率为0.04(4%)
1.首付款是房价的0.25(25
1.你存钱买房子的费用是100万美元。
下面是我的原始代码:

  1. annual_salary=float(input("Enter your annual salary:"))
  2. semi_annual_raise=0.07
  3. total_cost=1000000
  4. portion_down_payment = 0.25
  5. current_saving=0
  6. r=0.04
  7. low=0
  8. high=10000
  9. guess=(low+high)/2
  10. numGuesses=0
  11. while abs(portion_down_payment*total_cost-current_saving)>=100:
  12. current_saving=0
  13. portion_saved=guess/10000
  14. for i in range (36):
  15. current_saving+=current_saving*r/12+annual_salary/12*portion_saved
  16. if i%6==0 and i>0:
  17. annual_salary+=annual_salary*semi_annual_raise
  18. if portion_down_payment*total_cost-current_saving>0:
  19. low=guess
  20. else:
  21. high=guess
  22. guess=(low+high)/2
  23. numGuesses+=1
  24. if numGuesses > 1000:
  25. break
  26. if numGuesses>1000:
  27. print("It is not possible to pay the down payment in three years.")
  28. else:
  29. rate=guess/10000
  30. print("steps in bisection search is",numGuesses,"and the best saving rate is",rate)

当输入150000作为annual_salary时,结果为:

  1. It is not possible to pay the down payment in three years.

因为它不会返回正确的答案,我使用了一个函数来计算curring节省,代码如下所示:

  1. annual_salary=float(input("Enter your annual salary:"))
  2. semi_annual_raise=0.07
  3. total_cost=1000000
  4. portion_down_payment = 0.25
  5. r=0.04
  6. low=0
  7. high=10000
  8. guess=(low+high)/2
  9. numGuesses=0
  10. def current_saving(annual_salary,guess,months):
  11. portion_saved=guess/10000
  12. saving=0
  13. r=0.04
  14. semi_annual_raise=0.07
  15. for i in range (months):
  16. saving+=saving*r/12+annual_salary/12*portion_saved
  17. if i%6==0 and i>0:
  18. annual_salary+=annual_salary*semi_annual_raise
  19. return(saving)
  20. total=0
  21. while abs(portion_down_payment*total_cost-total)>=100:
  22. total=current_saving(annual_salary,guess,36)
  23. if portion_down_payment*total_cost-total>0:
  24. low=guess
  25. else:
  26. high=guess
  27. guess=(low+high)/2
  28. numGuesses+=1
  29. if numGuesses > 1000:
  30. break
  31. if numGuesses>1000:
  32. print("It is not possible to pay the down payment in three years.")
  33. else:
  34. rate=guess/10000
  35. print("steps in bisection search is",numGuesses,"and the best saving rate is",rate)

当这次输入150000作为annual_salary时,结果是:

  1. steps in bisection search is 7 and the best saving rate is 0.44140625

我觉得这两个版本基本上做同样的事情,为什么第一个不能工作,但第二个可以呢?我假设while循环中有什么错误,但我不能确定我的错误。再次感谢您的帮助!

u0sqgete

u0sqgete1#

在不起作用的版本中,不需要在while循环的每次迭代中重置annual_salary
在有效的版本annual_salary中,每次调用方法时都会重置局部变量。
让坏掉的那台机器运转起来

  1. ## save the original value
  2. annual_salary_original = annual_salary
  3. while abs(portion_down_payment * total_cost - current_saving) >= 100:
  4. ## reset salary on each iteration
  5. annual_salary = annual_salary_original
  6. ...

相关问题