124k views
4 votes
For the values "annual_salary = 120000, saving_rate = 0.3, house_price = 1000000" it is giving an answer of 230,000 and 62 months which is WRONG as it should be 99 months. However for the values "annual_salary = 100000, saving_rate = 0.2, house_price = 600000" it is giving the CORRECT answer of 138,000 and 107 months. what is wrong with the code?

down_payment_rate = 0.2
stamp_duty_rate = 0.03
tax_rate = 0.3
monthly_return_rate = 0.002
def cal_upfront():
upfront = (down_payment_rate*house_price)+(stamp_duty_rate*house_price)
return upfront
def cal_month_to_save_upfront(up_front):
monthly_saving = (((annual_salary * (1 - tax_rate)) * saving_rate) / 12)
month_count = 0
save_amount = 0
while save_amount < upfront:
save_amount = save_amount * (1 + monthly_return_rate)
save_amount += monthly_saving
month_count += 1
return month_count

User Neno
by
8.2k points

1 Answer

6 votes

Final answer:

The issue with the code is that the 'upfront' variable is not defined. By calling the 'cal_upfront()' function and assigning its value to the 'upfront' variable, the code will provide the correct answer.

Step-by-step explanation:

The issue with the code lies in the calculation of the 'upfront' variable. The 'upfront' variable is not defined in the provided code, which results in an error. To fix this, you need to call the 'cal_upfront()' function and assign its value to the 'upfront' variable in the 'cal_month_to_save_upfront()' function.

Here is the modified code:

def cal_month_to_save_upfront(up_front):
upfront = cal_upfront() # Call the cal_upfront() function to calculate upfront
monthly_saving = (((annual_salary * (1 - tax_rate)) * saving_rate) / 12)
month_count = 0
save_amount = 0
while save_amount < upfront:
save_amount = save_amount * (1 + monthly_return_rate)
save_amount += monthly_saving
month_count += 1
return month_count

By making this change, the first scenario with values 'annual_salary = 120000, saving_rate = 0.3, house_price = 1000000' should now provide the correct answer of 99 months.