mardi 27 octobre 2020

MIT OCW 6.0001 pset 1b, calculate the months of saving required.3 tests, first is correct, the other two are 1 month more than my code gives, why?

#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:', current_savings)

#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 semi­annual 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 semi­annual 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 semi­annual 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.

Aucun commentaire:

Enregistrer un commentaire