this is the exercise that work i on: 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:
Test Case 1
Enter your annual salary: 120000, Enter the percent of your salary to save, as a decimal: .10, Enter the cost of your dream home: 1000000, Number of months: 183
Test Case 2
Enter your annual salary: 80000, Enter the percent of your salary to save, as a decimal: .15, Enter the cost of your dream home: 500000, Number of months: 105, this is my code:
#variables and inputs:
current_savings=0
r=0.04
additional_current_savings=current_savings*r/12
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: "))
portion_down_payment=0.25*total_cost
monthly_salary=annual_salary / 12
new_total_saving=0
number_of_months=0
total_saving=portion_saved+current_savings+additional_current_savings
#a program to calculate how many months it will take someone to save up enough money for a down payment:
while new_total_saving != portion_down_payment :
new_total_saving += total_saving
number_of_months += 1
print(f"Number of months: {number_of_months}")
this is the expected output: Enter your annual salary: 120000 Enter the percent of your salary to save, as a decimal: .10 Enter the cost of your dream home: 1000000 Number of months: 183
i know that my program isn't all encompassing,but it should have solved atleast test case 1. i don't know why the terminal keep running without any result. Thanks in advance.
In the code, the while loop condition was new_total_saving != portion_down_payment
. The issue here is that due to the nature of floating-point arithmetic, it's possible that new_total_saving
never exactly equals portion_down_payment
due to small precision errors. Hence, the loop will run indefinitely because new_total_saving
will never be exactly equal to portion_down_payment
. So, your terminal seems running because the loop never terminated.
What you really want according to your problem statement is that your loop should run until new_total_saving
becomes greater than or equal to portion_down_payment
so that you have at least the down payment money saved. To fix this issue, you should use a less than inequality operand (<
).
Additionally in your program,
additional_current_savings
, new_total_saving
, and total_saving
)current_savings
is less than portion_down_payment
.current_savings
within the loop to properly account for both investment returns and monthly savings as following:while current_savings < portion_down_payment:
# Update current_savings with investment return
current_savings += current_savings * r / 12
# Add monthly savings from salary to current_savings
current_savings += monthly_salary * portion_saved
# Increment the number of months
number_of_months += 1