# Having trouble of assignment in MIT OpenCourseWare

I’m doing a course in MIT OpenCourseWare is known as Introduction to Computer Science and Programming in Python.
I’m facing a problem while I’m trying to solve the problem of the assignment section, Problem Set 1(PDF)(Download this pdf or click Details to see the problem).

Details

Problem Set 1
Handed out: Monday, September 12, 2016.
Due: 11:59 PM, Tuesday, September 20, 2016
This problem set will introduce you to using control flow in Python and formulating a computational
solution to a problem. It will also give you a chance to explore bisection search. This problem set has
three problems. You should save your code for the first problem as ps1a.py, the second problem as
ps1b.py and the third problem as ps1c.py, and make sure to hand in all three files. Don’t forget to
Collaboration
You may work with other students; however, each student should write up and hand in his or her
assignment separately. Be sure to indicate with whom you have worked in a comment at the start of
each file.
Before You Start: Read the Style Guide
Read the style guide sections 1, 2, and 3.

Part A: House Hunting

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.
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)
Hints
To help you get started, here is a rough outline of the stages you should probably follow in writing your
code:
● Retrieve user input. Look at input() if you need help with getting user input. For this problem set,
you can assume that users will enter valid input (e.g. they won’t enter a string when you expect
an int)
● Initialize some state variables. You should decide what information you need. Be careful about
values that represent annual amounts and those that represent monthly amounts.
Try different inputs and see how long it takes to save for a down payment. Please make your
program print results in the format shown in the test cases below.

Test Case 1

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 the percent of your salary to save, as a decimal: .15
Enter the cost of your dream home: 500000
Number of months: 105

Part B: Saving, with a raise

Background
In Part A, we unrealistically assumed that your salary didn’t change. But you are an MIT graduate, and
clearly you are going to be worth more to your company over time! So we are going to build on your
solution to Part A by factoring in a raise every six months.
In ps1b.py, copy your solution to Part A (as we are going to reuse much of that machinery). Modify
your program to include the following

1. Have the user input a semi-annual salary raise semi_annual_raise (as a decimal percentage)
2. After the 6th month, increase your salary by that percentage. Do the same after the 12
th
th
month, the 18 month, and so on.
Write a program to calculate how many months it will take you save up enough money for a down
payment. LIke before, assume that your investments earn a return of r = 0.04 (or 4%) and the
required down payment percentage is 0.25 (or 25%). Have the user enter the following variables:
3. The starting annual salary (annual_salary)
2
4. The percentage of salary to be saved (portion_saved)
5. The cost of your dream home (total_cost)
6. The semi­annual salary raise (semi_annual_raise)
Hints
To help you get started, here is a rough outline of the stages you should probably follow in writing your
code:
● Retrieve user input.
● Initialize some state variables. You should decide what information you need. Be sure to be
careful about values that represent annual amounts and those that represent monthly amounts.
● Be careful about when you increase your salary – this should only happen after the 6th, 12
th, 18
th
month, and so on.
Try different inputs and see how quickly or slowly you can save enough for a down payment. Please
make your program print results in the format shown in the test cases below.

Test Case 1

Enter your starting annual salary: 120000
Enter the percent of your salary to save, as a decimal: .05
Enter the cost of your dream home: 500000
Enter the semi­annual raise, as a decimal: .03
Number of months: 142

Test Case 2

Enter your starting annual salary: 80000
Enter the percent of your salary to save, as a decimal: .1
Enter the cost of your dream home: 800000
Enter the semi­annual raise, as a decimal: .03
Number of months: 159

Test Case 3

Enter your starting annual salary: 75000
Enter the percent of your salary to save, as a decimal: .05
Enter the cost of your dream home: 1500000
Enter the semi­annual raise, as a decimal: .05
Number of months: 261

I have an issue with Part B.

Here is my code to solve Part B,

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: "))
semi_annual_raise = float(input("Enter the semi­annual raise, as a decimal: "))
portion_down_payment = total_cost * 0.25
monthly_salary = annual_salary/12
portion_saved_monthly = monthly_salary * portion_saved
current_savings = 0
months = 0
while current_savings < portion_down_payment:
if months % 6 == 0:
annual_salary = annual_salary + (annual_salary*semi_annual_raise)
monthly_salary = annual_salary/12
portion_saved_monthly = monthly_salary * portion_saved
current_savings = current_savings + (current_savings*(0.04/12)) + portion_saved_monthly
months = months + 1
print("Number of months: ", months)

My Output:

Enter your starting annual salary: 120000
Enter the percent of your salary to save, as a decimal: .05
Enter the cost of your dream home: 500000
Enter the semi­annual raise, as a decimal: .03
Number of months: 139

expectation:

Enter your starting annual salary: 120000
Enter the percent of your salary to save, as a decimal: .05
Enter the cost of your dream home: 500000
Enter the semi­annual raise, as a decimal: .03
Number of months: 142

Please let me know where I’m making mistakes.

Take a closer look at the part applying raise, see if it’s doing what it should and when it should.

1 Like

Thank you @sanity
It’s fixed my issue.
Here is the change,

while current_savings < portion_down_payment:
current_savings = current_savings + (current_savings*(0.04/12)) + portion_saved_monthly
months = months + 1
if months % 6 == 0:
annual_salary = annual_salary + (annual_salary*semi_annual_raise)
monthly_salary = annual_salary/12
portion_saved_monthly = monthly_salary * portion_saved

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.