Expected output for MIT OCW problem set 1 is off by 1 for one of the test cases

Viewed 62

I'm currently doing the MIT OCW problem set 1 listed below and I'm pretty much done with the problem except I am having some trouble with the output with one of the test cases.

You have graduated from MIT and now have a great job! You move to the San Francisco Bay Area and decide that you want to start saving to buy a house. As housing prices are very high in the Bay Area, you realize you are going to have to save for several years before you can afford to make the down payment on a house. In Part A, we are going to determine how long it will take you to save enough money to make the down payment given the following assumptions:

  1. Call the cost of your dream home total_cost.
  2. Call the portion of the cost needed for a down payment portion_down_payment. For simplicity, assume that portion_down_payment = 0.25 (25%).
  3. Call the amount that you have saved thus far current_savings. You start with a current savings of $0.
  4. Assume that you invest your current savings wisely, with an annual return of r (in other words, at the end of each month, you receive an additional current_savings*r/12 funds to put into your savings – the 12 is because r is an annual rate). Assume that your investments earn a return of r = 0.04 (4%).
  5. Assume your annual salary is annual_salary.
  6. Assume you are going to dedicate a certain amount of your salary each month to saving for the down payment. Call that portion_saved. This variable should be in decimal form (i.e. 0.1 for 10%).
  7. At the end of each month, your savings will be increased by the return on your investment, plus a percentage of your monthly salary (annual salary / 12). Write a program to calculate how many months it will take you to save up enough money for a down payment. You will want your main variables to be floats, so you should cast user inputs to floats.
    1 Your program should ask the user to enter the following variables:
  8. The starting annual salary (annual_salary)
  9. The portion of salary to be saved (portion_saved)
  10. The cost of your dream home (total_cost)

Here is what I have so far for my code

portion_down_payment = 0.25
r = 0.04

annual_salary = float(input("Enter your annual salary: "))
portion_saved = float(input("Enter the percent of your salary to save, as a decimal: "))
total_cost = float(input("Enter the cost of your dream home: "))

months = 0
money_saved = 0

while money_saved < (total_cost*portion_down_payment):
    months += 1
    money_saved += annual_salary*portion_saved/12
    interest = (money_saved*r)/12
    money_saved += interest


print(f"Number of months: {months}")

one of the test cases for input was:120,000 for annual_salary 0.1 for portion_saved and 1000000 for total_cost. The expected output was

Number of months: 183

but the actual output for my code is

Number of months: 182
1 Answers

The order in your while loop is the reason for the error. In particular, the interest should be calculated before increasing that month's money_saved:

Change this:

months += 1
money_saved += annual_salary*portion_saved/12
interest = (money_saved*r)/12
money_saved += interest

To this:

months += 1
interest = (money_saved*r)/12
money_saved += annual_salary*portion_saved/12
money_saved += interest
Related