 # Needing help with while looping

I’m having trouble getting my counter to multiply “current savings”- (variable) with number of months (variable).

``````monthly_salary = annual_salary/12
portion_saved = monthly_salary*float(input("Enter the percentage of salary to save, as a decimal:"))
r = float(0.04)
total_cost = float(input("Enter the cost of your dream home:"))
portion_down_payment = float(total_cost*.25)
number_of_months = 1
current_savings = portion_saved * number_of_months + (portion_saved * (r/12))
while current_savings < portion_down_payment:
number_of_months += 1
print ("number of months:",int(number_of_months))
``````

In my variable explorer, my counter is working fine. Number of months counts up but current savings stays the same. Why would I need to redefine “current savings” within the loop?

``````monthly_salary = annual_salary/12
portion_saved = monthly_salary*float(input("Enter the percentage of salary to save, as a decimal:"))
r = float(0.04)
total_cost = float(input("Enter the cost of your dream home:"))
portion_down_payment = float(total_cost*.25)
number_of_months = 1
current_savings = portion_saved * number_of_months + (portion_saved * (r/12))
print (current_savings) #1003.33333334
while current_savings < portion_down_payment:
number_of_months += 1
current_savings = (current_savings * (r/12)) + current_savings
print ("number of months:",int(number_of_months))
``````

When I redefine within the loop it at least works in conjunction with each other. Although my math is clearly off unfortunately.

The print (current savings) in line 8 is for reference only and will be deleted at the end.

r = 0.04 is an annual interest rate.

I am using the following as inputs for reference.
120,000 salary
.10 percentage of salary
1,000,000 cost of dream home

I see where my math was wrong now but back to the main question at hand. Is this the only way of implementing this loop or I am adding more steps than necessary. Here is the code with the correct math.

``````monthly_salary = annual_salary/12
portion_saved = monthly_salary*float(input("Enter the percentage of salary to save, as a decimal:"))
r = float(0.04)
total_cost = float(input("Enter the cost of your dream home:"))
portion_down_payment = float(total_cost*.25)
number_of_months = 1
current_savings = portion_saved * number_of_months + (portion_saved * (r/12))
print (current_savings) #1003.33333334
while current_savings < portion_down_payment:
number_of_months += 1
current_savings = (current_savings * (r/12)) + current_savings + portion_saved
print (current_savings)
print ("number of months:",int(number_of_months))
``````

Hi Rufus,

You don’t need to say `r = float(0.04)`, since 0.04 (4%) is already a
float. This is sufficient: `r = 0.04`.

Likewise `float(total_cost*.25)` doesn’t need the call to float() and
can be just written as `total_cost*.25`.

(You only need to call float() to change a string to a floating point
number.)

Likewise the call at the very end to `int(number_of_months)`. You don’t
need to call int() because the value `number_of_months` is already an
int.

I think you can simplify your loop like this. I haven’t checked the
results, so you should compare the results from your code and mine and
see that they give the same result:

``````number_of_months = 0
current_savings = 0.0
while current_savings < portion_down_payment:
number_of_months += 1
current_savings *= 1 + r/12  # Add monthly interest.
current_savings += portion_saved  # Add monthly payment.
print(current_savings)

print("number of months:", number_of_months)
``````

Note that the line `current_savings *= 1 + r/12` is not a typo, it is
asterisk-equals, not plus-equals. It uses the fact that:

``x + x*r/12 == x*(1 + r/12)``

Thank you for replying to this! I changed the unnecessary casting. I also tried the code that you posted here to see if the results were the same. Your code does work, and it produces the same total “number of months” as the code I wrote, but the math differs slightly.

I also tried it with the updated code block below to check the math, as I may have added some unintended interest on the first iteration with this statement.

``````number_of_months = 1
current_savings = portion_saved * number_of_months + (portion_saved * (r/12))
``````

The following updated code produces less of a difference and seems as it could be correct to me.

``````annual_salary = float(input("Enter your annual salary:"))
monthly_salary = annual_salary/12
portion_saved = monthly_salary*float(input("Enter the percentage of salary to save, as a decimal:"))
r = (0.04)
total_cost = float(input("Enter the cost of your dream home:"))
portion_down_payment = total_cost*.25
number_of_months = 0
current_savings = 0
print (current_savings)
while current_savings < portion_down_payment:
number_of_months += 1
current_savings = (current_savings * (r/12)) + current_savings + portion_saved
print (current_savings)
print ("Number of months:",(number_of_months))
``````

My first result:
251575.724525696
Number of months: 183