MIT 6.100L Introduction to CS and Programming Using Python, Fall 2022 · Problem set 1, 2) Part A: Saving for a House — ps1a.py · Dr. Ana Bell · CC BY-NC-SA 4.0 · MIT publishes no solution for this set; the worked solution is ours
Write the body of months_to_save(yearly_salary, portion_saved, cost_of_dream_home): MIT's Part A program, which finds the number of months it takes to save up for the down payment on your dream home.
No input() here. MIT's program asks the user for three numbers with input(). In the browser the starter wraps the program in a function instead, just as MIT's own tester does: the three parameters on the def line, yearly_salary, portion_saved and cost_of_dream_home, stand in for the three input() calls (in MIT's order) and already hold the numbers the user typed. Write your program between the def line and the final return months, using those three names exactly as you would have used the values input() returned. The last line, return months, hands your months back to the tests. Don't change the def line or the return line.
You have just graduated from MIT and have a job! You move to the Bay Area and decide that you want to start saving for a home. Houses are fairly expensive, so you start saving up for the down payment on your dream home.
Your goal is to find the number of months it takes to save up for a down payment. The cost of your down payment is calculated by multiplying the total cost of your dream house by the down payment percentage.
User Inputs. MIT's program asks the user to enter the following variables and cast them as floats, in the following order, at the beginning of the program, before declaring other variables. Here they are the function's parameters, already filled in:
yearly_salary)portion_saved). This variable should be in decimal form (e.g. 0.1 for 10%).cost_of_dream_home)Writing the Program. You will need to determine how many months it will take given the following information:
yearly_salary, as described above.portion_saved, as described above.cost_of_dream_home, as described above.portion_down_payment, the percentage of the total cost needed for a down payment. Assume portion_down_payment = 0.25 (25%).amount_saved, which starts at $0.r. In other words, at the end of each month, you receive an additional amount_saved * (r/12) funds for your savings (the 12 is because r is an annual rate). Assume r = 0.05 (5%).Output. Your program should store the number of months required to save for the down payment using a variable called months.
Notes
MIT's three test cases, as calls (each prints the months your function returns):
print(months_to_save(112000, .17, 750000)) # 97 print(months_to_save(65000, .20, 400000)) # 79 print(months_to_save(350000, .3, 10000000)) # 189
Adapted for the browser: MIT's three input() calls became the parameters of months_to_save (same names, same order), and the program returns months instead of printing Number of months: ....