简体   繁体   English

有人可以向我解释为什么这段代码不起作用吗?

[英]Can someone explain to me why doesn't this piece of code work?

annual_salary = int(input("Your annual salary "))
semi_annual_raise = 0.07
r = 0.04
down_payment = 250000
epsilon = 100
low = 0
high = 10000
guess = (high + low)//2
best_saving_rate = (guess/10000)
months = 0
current_savings = 0
steps = 0

while abs(current_savings - down_payment) >= 100:
    for i in range(36):
        current_savings += best_saving_rate*(annual_salary/12) + (current_savings*r)/12
        months +=1
        if months%6 == 0:
            annual_salary = annual_salary + semi_annual_raise*annual_salary  
    if current_savings < down_payment:
        low = guess
    else:
        high = guess
    steps += 1
    guess = (high + low)//2
    best_saving_rate = float(guess/10000)
print(steps)
print(best_saving_rate)
print(current_savings)

This code is supposed to find the best saving rate for someone who is trying to have enough money for a payment of 250000 dollars in 36 months.该代码应该为试图在 36 个月内有足够的钱支付 250000 美元的人找到最佳储蓄率。 I use bisection search and I think I'm in the right track but it won't work.我使用二分搜索,我认为我在正确的轨道上,但它不会工作。 I think the problem is that the variable current savings is not reinitializing with every iteration and I do not know how to make it do that.我认为问题在于可变电流节省不会在每次迭代时重新初始化,我不知道如何让它做到这一点。 PLEASE HELP.请帮忙。

Why are you expecting the current_savings to be reset to 0 with every iteration?为什么您希望每次迭代时current_savings都重置为0 You do not do that in the code, so what would cause that to happen?您没有在代码中这样做,那么什么会导致这种情况发生呢? Also by the looks of the code, you should be resetting months to 0 as well (though it appears that the for loop index variable i should actually be the month).同样从代码的外观来看,您也应该将months重置为0 (尽管看起来for循环索引变量i实际上应该是月份)。

This fixes the obvious errors that I could see:这修复了我可以看到的明显错误:

while abs(current_savings - down_payment) >= 100:
    current_savings = 0   # add this
    for month in range(36):  # Change this
        current_savings += best_saving_rate * (annual_salary / 12) + (current_savings * r) / 12
        # months += 1  <-- remove this
        if months % 6 == 0:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人来解释为什么这段代码对我不起作用? - Come someone explain why this code won't work for me? 有人可以解释为什么 python 中的 var = input() 不起作用吗? - Can someone explain why var = input() in python doesn't work? 有人可以在这段代码中解释 _siftup 和 _siftdown 吗? - Can someone explain _siftup and _siftdown in this piece of code? 有人可以向我解释为什么 &#39;\\n&#39; 在与函数一起使用时没有注册吗? - Can someone explain to me why '\n' doesn't register when it is used in with a function? 我是python的初学者,有人可以详细解释一下这段代码吗? - I am a beginner in python, can someone please explain me this piece of code in detail? 试图获取HTTP代码。 有人可以在他们的Python解释器中为我尝试这个代码,看看为什么它不起作用? - Trying to get HTTP code. Can someone try this code for me in their Python interpretor and see why it doesn't work? 有人可以解释为什么替换对我不起作用 - Can someone explain why replace isn't working for me 有人可以解释这个python代码如何不会给出错误 - can someone explain how this python code doesn't give an error 有人可以为我解释这个Python代码吗? - Can someone explain this Python code for me? 有人可以为我解释 statsmodels 代码问题吗? - Can someone explain the statsmodels code question for me?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM