The exercise program is from MIT OpenCourseWare. It asks me to calculate how many months can a down payment is fulfilled, with three portion:1. investment. 2. Part of the salary. 3. The salary gains a semi-year raise: this should only happen after the 6th, 12th, 18th month, and so on.
Here is my work:
current_saving = 0
annual_salary = int(input("Enter your annual salary:"))
portion_saved = float(
input("Enter the percent of your salary to save, as a decimal:"))
total_cost = int(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
months = 0
while current_saving <= portion_down_payment:
if months % 6 !=1:
current_saving = current_saving * 0.04 /12 + annual_salary/12 * portion_saved + current_saving
months +=1
else:
annual_salary = annual_salary*(1+semi_annual_raise)
current_saving = current_saving * 0.04 /12 + annual_salary/12 * portion_saved + current_saving
months +=1
print(months)
This is my own run and sample test provided by an official document by MIT OpenCourseWare.
My own run
Please help me, thank you! I will get to interact as soon as possible.
Your issue is in the computation of every 6 month with the modulo. What you are doing is that you increase the salary everytime the month is in the form 6n + 1. Which means you increase the salary on month 1, 7, 13 etc. (as your index starts at zero, you are increasing the salary after the first month).
So you have a raise too early and the other ones too late.
Note: You have the same code in you if and else part, put it outside for easier to read and maintain code.
while current_saving <= portion_down_payment:
# Apply the raise every time the month is a multiple of 6 (as you start from 0, you need the +1 here)
if (months + 1) % 6 == 0:
annual_salary = annual_salary*(1+semi_annual_raise)
current_saving = current_saving * 0.04 /12 + annual_salary/12 * portion_saved + current_saving
months +=1
Related
I am an absolute beginner in programming (just started yesterday and my major is not CS). I have been struggling with this while loop as I don't know why this print statement is not executed(sometimes it prints as "1"). Please tell me where is wrong. Thanks, guys.
current_saving = 0
# This program intends to calculate the how many months the down
# portion can be paid
annual_input = int(input("Enter your annual salary:"))
portion_saved = float(
input("Enter the percent of your salary to save, as a decimal:"))
total_cost = int(input("Enter the cost of your dream home:"))
portion_down_payment = total_cost * 0.25
months = 0
while current_saving <= portion_down_payment:
current_saving = annual_input / 12 * portion_saved + current_saving * 0.04 / 12
months = months + 1
print(months)
Modify the code to accumulate savings.
current_saving = 0
# This program intends to calculate the how many months the down
# portion can be paid
annual_input = int(input("Enter your annual salary:"))
portion_saved = float(
input("Enter the percent of your salary to save, as a decimal:"))
total_cost = int(input("Enter the cost of your dream home:"))
portion_down_payment = total_cost * 0.25
months = 0
while current_saving <= portion_down_payment:
# Accumulate savings
current_saving = current_saving + annual_input / 12 * portion_saved + \
current_saving * 0.04 / 12
print(f'{current_saving=}') # Comment out this line to not print the accumulation
months = months + 1
print(months)
Test Reults
Enter your annual salary:5000
Enter the percent of your salary to save, as a decimal:.5
Enter the cost of your dream home:10000
current_saving=208.33333333333334
current_saving=417.36111111111114
current_saving=627.0856481481482
current_saving=837.5092669753087
current_saving=1048.6342978652262
current_saving=1260.4630788581103
current_saving=1472.9979557876372
current_saving=1686.2412823069292
current_saving=1900.195419914619
current_saving=2114.862737981001
current_saving=2330.245613774271
current_saving=2546.3464324868523
12
I am trying to calculate the end balance earned in one year based on $1000 monthly contributions and $0 starting balance on a 5% annual return.
So each year I should be contributing $12,000 and the interest that I earn should have equaled $322.58 making the end balance $12,322.58.
Here is my code so far but the end balance that I am getting is only 12050.
count = 0
current_savings = 0
r = 0.05
while count < 12:
current_savings += 1000
Monthly_interest = (current_savings*r)/12
Final_amount = current_savings + Monthly_interest
count += 1
print(Final_amount)
Note that most banks will compute the interest BEFORE adding the new deposit. You'll have to decide what order is right for you.
current_savings = 0
r = 0.05
for _ in range(12):
current_savings += 1000
Monthly_interest = (current_savings*r)/12
current_savings += Monthly_interest
print(current_savings)
I am new to programming and I am stuck with one of my programming assignments. please evaluate the given code and let me know the mistakes I have made. the code is expected to output the number of months required to save up to buy a new house...
total_cost = float(input("cost of house:"))
portion_down_payment= 0.25
current_savings = 0.0
r = .04
annual_salary = float(input("your annual salary is:"))
portion_saved = float(input("portion of income saved:"))
months = 0
while current_savings <= portion_down_payment*total_cost:
current_savings = current_savings*r/12 + portion_saved*annual_salary/12
months = months+1
print("To buy your dream house you gotta wait for",months, "months")
You likely want to add to your savings, not overwrite them:
current_savings = current_savings*r/12 + portion_saved*annual_salary/12
should be:
current_savings = current_savings + current_savings*r/12 + portion_saved*annual_salary/12
#Code
'''
annual_salary = float(input('What is your starting annual salary?'))
monthly_salary = annual_salary/12
portion_saved = float(input('What percentage of your salary will you save each month(in decimals)?'))
Total_cost = float(input('How much is your dream home?'))
semi_annual_raise = float(input('What is your raise every 6 months(in decimals)?'))
portion_down_payment = 0.25*Total_cost
r = 0.04
current_savings = 0
months = 0
while current_savings < portion_down_payment:
months += 1
if months % 6 == 0:
monthly_salary += semi_annual_raise*monthly_salary
current_savings += portion_saved*monthly_salary + current_savings*r/12
print('Number of months:', months)
#Tests
'''
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 semiannual raise, as a decimal: .03
Number of months: 142
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 semiannual raise, as a decimal: .03
Number of months: 159
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 semiannual raise, as a decimal: .05
Number of months: 261
here are the 3 tests, please help
the last 2 give 158 and 260 respectively for my codes results
it is an algorithm to calculate the months needed to save to pay a houses downpayment in accordance to salary, semi-annual raise, amount to save monthly, and a return on investments.
#Code
annual_salary = float(input('What is your starting annual salary?'))
monthly_salary = annual_salary/12
portion_saved = float(input('What percentage of your salary will you save each month(in decimals)?'))
Total_cost = float(input('How much is your dream home?'))
semi_annual_raise = float(input('What is your raise every 6 months(in decimals)?'))
portion_down_payment = 0.25*Total_cost
r = 0.04
current_savings = 0
months = 0
while current_savings < portion_down_payment:
current_savings += portion_saved*monthly_salary + current_savings*r/12
months += 1
if months % 6 == 0:
monthly_salary += semi_annual_raise*monthly_salary
print('Number of months:', months)
#misplacement of the months+=1 caused the error
The issue is that you're salary increase happens before your savings accrue interest. The assignment says to have the increase happen after the 6th month.
So I am attempting to implement a bisection search algorithm in Python that returns an "optimal" savings rate.
I've tried creating several different functions, and I don't understand why the program gets caught in an infinite loop. I do know that the abs(current_savings - down_payment) is what causes the recursive infinite loop but I do not know why.
First things first, this doesn't really explain why my program doesn't work but here goes:
At the end of each month I earn interest on current savings, which is
applied first, and then I receive my monthly salary, which is just
1/12 of my annual salary.
I am attempting to find the best rate to apply to my monthly salary, to then add to my current savings.
My first function simply checks to see if one's salary is high enough to ever save for the 250K down payment. If their salary is not high enough, it prints that it is not adequate and returns False.
My second function attempts to find the best rate ("portion saved"), or the best rate to save of monthly salary in order to fall within 100 dollars of the down_payment. In addition, I must record the number of "steps" my bisection search function takes to find the optimal rate.
Here is the code:
#Givens
annual_salary = 150000
initial_salary = annual_salary
interest_rate = float(0.04/12.0)
down_payment = float(250000.0)
semi_annual_raise = 0.07
#Bisect-search
low = float(0.0)
high = float(10000.0)
portion_saved = float((low+high)/2)
current_savings = 0
months = 0
steps = 0
def isPossible(annual_salary):
count = 0
current_savings = 0
while count < 36:
current_savings += (current_savings*interest_rate) + (annual_salary/12)
count += 1
if count % 6 == 0:
annual_salary += (annual_salary*semi_annual_raise)
if current_savings < down_payment:
print("It is not possible to pay the down payment in three years.")
return False
else:
return True
def bisearch(initial_salary,interest_rate,down_payment,semi_annual_raise,low,high,portion_saved,steps):
current_savings = 0
months = 0
while abs(current_savings - down_payment) > 100.0:
months = 0
current_savings = 0
while months < 36:
current_savings = current_savings + (initial_salary*interest_rate)
current_savings = current_savings + (initial_salary/12*portion_saved/10000.0)
months += 1
if months % 6 == 0:
initial_salary += (initial_salary*semi_annual_raise)
steps += 1
if current_savings > down_payment:
high = portion_saved
else:
low = portion_saved
portion_saved = ((low+high)/2.0)
print("Best saving rate: ", (portion_saved/10000.0))
print("Steps in bisection search: ", steps)
if isPossible(annual_salary) == True:
bisearch(initial_salary,interest_rate,down_payment,semi_annual_raise,low,high,portion_saved,steps)
And the test cases:
Note: the number of bisection search steps doesn't have to be the same, but the rate should be the same
Test Case 1
Enter the starting salary: 150000
Best savings rate: 0.4411
Steps in bisection search: 12
Test Case 2
Enter the starting salary: 300000
Best savings rate: 0.2206
Steps in bisection search: 9
If anyone could help me out I would greatly appreciate it, been at this for hours trying to come up with a fix.
I was confused in the same problem and finally found a solution. Try resetting initial_salary back to annual_salary inside the first while loop within your bisection function, else that would just keep on increasing every time you hit six months on the inner loop. Does that work?