No, the current code does not factor in details such as rent growth, selling costs, and other expenses associated with selling a house.
Here's an updated code that includes these factors:
def calculate_mortgage_payment(principal, annual_interest_rate, years):
"""
Calculate the monthly mortgage payment.
:param principal: The initial amount borrowed.
:param annual_interest_rate: The annual interest rate as a decimal.
:param years: The number of years to repay the mortgage.
:return: The monthly mortgage payment.
"""
monthly_interest_rate = annual_interest_rate / 12
number_of_payments = years * 12
monthly_payment = principal * monthly_interest_rate * (1 + monthly_interest_rate) ** number_of_payments / ((1 + monthly_interest_rate) ** number_of_payments - 1)
return monthly_payment
def calculate_outstanding_mortgage_balance(principal, annual_interest_rate, years, monthly_payment, payments_made):
"""
Calculate the outstanding mortgage balance.
:param principal: The initial amount borrowed.
:param annual_interest_rate: The annual interest rate as a decimal.
:param years: The number of years to repay the mortgage.
:param monthly_payment: The monthly mortgage payment.
:param payments_made: The number of payments made.
:return: The outstanding mortgage balance.
"""
monthly_interest_rate = annual_interest_rate / 12
outstanding_balance = principal
for _ in range(payments_made):
interest = outstanding_balance * monthly_interest_rate
outstanding_balance = outstanding_balance + interest - monthly_payment
return outstanding_balance
def calculate_investment_return(initial_investment, monthly_investment, annual_return, years):
"""
Calculate the future value of an investment.
:param initial_investment: The initial amount invested.
:param monthly_investment: The monthly amount invested.
:param annual_return: The annual return as a decimal.
:param years: The number of years to invest.
:return: The future value of the investment.
"""
monthly_return = (1 + annual_return) ** (1 / 12) - 1
number_of_payments = years * 12
future_value = initial_investment
for _ in range(number_of_payments):
future_value = (future_value + monthly_investment) * (1 + monthly_return)
return future_value
def main():
print("Mortgage vs Renting Calculator")
print("--------------------------------")
# Get user input
purchase_price = float(input("Enter the purchase price of the house: $"))
down_payment = float(input("Enter the down payment as a percentage of the purchase price (e.g., 20 for 20%): "))
down_payment_amount = purchase_price * (down_payment / 100)
mortgage_amount = purchase_price - down_payment_amount
annual_interest_rate = float(input("Enter the annual interest rate as a percentage (e.g., 4 for 4%): ")) / 100
property_taxes = float(input("Enter the annual property taxes as a percentage of the purchase price (e.g., 1.25 for 1.25%): ")) / 100
insurance = float(input("Enter the annual insurance premium: $"))
rent = float(input("Enter the monthly rent: $"))
annual_rent_growth = float(input("Enter the annual rent growth as a percentage (e.g., 3 for 3%): ")) / 100
years_to_sell = int(input("Enter the number of years to sell the house: "))
annual_appreciation = float(input("Enter the annual appreciation in the house value as a percentage (e.g., 3 for 3%): ")) / 100
selling_costs = float(input("Enter the selling costs as a percentage of the sale price (e.g., 6 for 6%): ")) / 100
annual_investment_return = float(input("Enter the annual return on investment as a percentage (e.g., 7 for 7%): ")) / 100
# Calculate mortgage payment
monthly_mortgage_payment = calculate_mortgage_payment(mortgage_amount, annual_interest_rate, 30)
# Calculate total monthly cost of owning the house
monthly_property_taxes = (property_taxes * purchase_price) / 12
monthly_insurance = insurance / 12
total_monthly_cost = monthly_mortgage_payment + monthly_property_taxes + monthly_insurance
# Check if renting is cheaper
monthly_investment = rent - total_monthly_cost
if monthly_investment < 0:
monthly_investment = -monthly_investment
print("Warning: Owning the house is cheaper than renting.")
# Calculate the future value of the house
future_house_value = purchase_price * (1 + annual_appreciation) ** years_to_sell
# Calculate the outstanding mortgage balance after 'years_to_sell' years
outstanding_mortgage_balance = calculate_outstanding_mortgage_balance(mortgage_amount, annual_interest_rate, 30, monthly_mortgage_payment, years_to_sell * 12)
# Calculate the profit from selling the house
selling_price = future_house_value
selling_expenses = selling_price * selling_costs
profit_from_selling = selling_price - selling_expenses - outstanding_mortgage_balance - purchase_price + down_payment_amount
# Calculate the future value of the investment
initial_rent = rent
total_investment = 0
for year in range(years_to_sell):
monthly_rent = initial_rent * (1 + annual_rent_growth) ** year
monthly_investment = monthly_rent - total_monthly_cost
if monthly_investment < 0:
monthly_investment = -monthly_investment
total_investment += monthly_investment * 12
future_investment_value = calculate_investment_return(0, total_investment / years_to_sell / 12, annual_investment_return, years_to_sell)
# Print the results
print("\nResults:")
print("--------")
print(f"Total monthly cost of owning the house: ${total_monthly_cost:.2f}")
print(f"Monthly investment: ${monthly_investment:.2f}")
print(f"Future value of the house after {years_to_sell} years: ${future_house_value:.2f}")
print(f"Outstanding mortgage balance after {years_to_sell} years: ${outstanding_mortgage_balance:.2f}")
print(f"Selling expenses: ${selling_expenses:.2f}")
print(f"Profit from selling the house: ${profit_from_selling:.2f}")
print(f"Future value of the investment after {years_to_sell} years: ${future_investment_value:.2f}")
if profit_from_selling > future_investment_value:
print("\nBuying the house and selling it after {} years is more profitable.".format(years_to_sell))
elif profit_from_selling < future_investment_value:
print("\nRenting and investing the difference is more profitable.")
else:
print("\nBoth options are equally profitable.")
if __name__ == "__main__":
main()