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

bassam ch
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. 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.

Glenn Mackintosh

Why are you expecting the current_savings to be reset to 0 with every iteration? 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).

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:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

can anyone explain to me why this lisp code doesn't work?

Can someone explain to me why the binding method doesn't work in these examples

Come someone explain why this code won't work for me?

Can someone explain to me what does 'as never' do in this piece of code?

Can someone explain why this code doesn't work? RE: string list manipulation with gross character length as criteria

Can someone please explain why does a piece of code work the same as mine?

I don't understand this piece of code, can someone explain it?

Could someone tell me why this piece of code doesn't call me that function?

Can someone tell me why my code doesn`t work with this condition?

Can someone tell me why this filter function doesn't work?

Can someone tell me why the a:hover doesn't work?

Can someone explain this piece of angularjs directive code?

Can someone explain what is happening in this piece of code?

Can someone explain what this piece of code is doing?

Can anyone explain why this brainfuck if/else code doesn't work?

Can someone explain to me why '\n' doesn't register when it is used in with a function?

js Array.prototype.filter.call() - can someone explain me how this piece of code works?

Can someone explain me why we used curly braces "{}" in this code?

I don't think I fully understand realloc, can someone explain to me why a realloc in my code sometimes fails but sometimes doesn't

can someone explain the python code to me, I don't understand

Can someone explain why this code does not work the way i want?

can anyone please explain me this piece of code

can anyone explain to me this piece of code in flutter?

Trying to make multiple navigation links move from left to right with a delay for each one, can someone explain why this code snippet doesn´t work?

Can somebody explain why this doesn't work?

Can someone explain the working of the statement in the provided piece of code?

can someone try to explain this code for me?

can someone explain me this code in java?

Strange code... Can someone explain me this