Search code examples
pythonwhile-loopvariable-assignment

Assigning formula to variable vs performing calulation in-line changes the outcome of my script using a while loop. Why?


I've encountered a problem that I think is related not to an error in my equation, but to the way that Python processes information. I don't understand and I'm hoping that someone can explain.

The following is a problem from MIT OCW 6.0001 in which a program calculates the number of months it will take to save for someone's dream home. The problem assumes that we invest carefully and achieve a rate of return of 4% annually on our savings. This is the variable r. The program should pass a test case by producing the result 183 months when the user inputs: 120000, .1, and 1000000.

This is my first attempt at the solution:

annual_salary = (float(input("What is your starting annual salary?")))
portion_saved = (annual_salary * (float(input("What porportion of your annual salary will you save? Please enter a percentage as a decimal:"))))/12
total_cost = (float(input("What is the cost of your dream home?")))

portion_down_payment = total_cost * 0.25
current_savings = 0.0
r = 0.04
monthly_return = ((current_savings*r)/12)
months = 0.0

while current_savings < portion_down_payment:
    current_savings += portion_saved + monthly_return
    months += 1
print("Number of months:", months)

It does not pass the test case. and I've narrowed down the problem to the monthly_return function. If I run a test by printing the monthly_return,while months < 2:

annual_salary = (float(input("What is your starting annual salary?")))
portion_saved = (annual_salary * (float(input("What porportion of your annual salary will you save? Please enter a percentage as a decimal:"))))/12
total_cost = (float(input("What is the cost of your dream home?")))

portion_down_payment = total_cost * 0.25
current_savings = 0.0
r = 0.04
monthly_return = ((current_savings*r)/12)
months = 0.0

while months < 2:
    current_savings += portion_saved + monthly_return
    months += 1
print("Number of months:", months)
print(monthly_return)

I find that the return is equal to 0.0.

However, if I simply remove the variable I've created for calculating monthly_return, and instead paste the formula contained within it into its space in the script, not only will I find that it passes the test case, but the monthly return calculation will also return ~6.68 while months < 2.

annual_salary = (float(input("What is your starting annual salary?")))
portion_saved = (annual_salary * (float(input("What porportion of your annual salary will you save? Please enter a percentage as a decimal:"))))/12
total_cost = (float(input("What is the cost of your dream home?")))

portion_down_payment = total_cost * 0.25
current_savings = 0.0
r = 0.04
months = 0.0

while months < 2:
    current_savings += portion_saved + ((current_savings*r)/12)
    months += 1
print("Number of months:", months)
print(((current_savings*r)/12))

I have to believe that what's happening here has something to do with the way that Python is reading each variable while executing the while loop, but I don't understand what's happening or why. Can you please explain?


Solution

  • You need to update those values in the while loop:

    # Example data
    annual_salary = 120000.0
    portion_saved = annual_salary * (0.1)/12
    total_cost = 1000000.0
    
    portion_down_payment = total_cost * 0.25
    current_savings = 0.0
    r = 0.04
    months = 0.0
    
    while current_savings < portion_down_payment:
        portion_down_payment = total_cost * 0.25
        monthly_return = ((current_savings*r)/12)
        current_savings += portion_saved + monthly_return
        months += 1
    print("Number of months:", months)
    

    Outputs:

    Number of months: 183.0
    

    Because the values portion_down_payment and monthly_return need to be updated on each iteration of the while loop. Rather than being assigned once.