File size: 2,698 Bytes
ad01213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
622a3fa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import streamlit as st
import numpy as np

# Function to calculate renting cost
def calculate_renting_cost(rent, annual_increase, years):
    total_rent = 0
    for year in range(years):
        total_rent += rent * 12
        rent += rent * (annual_increase / 100)
    return total_rent

# Function to calculate buying cost
def calculate_buying_cost(property_price, down_payment_percentage, mortgage_rate, loan_years, tax, maintenance):
    down_payment = property_price * (down_payment_percentage / 100)
    loan_amount = property_price - down_payment
    monthly_interest_rate = mortgage_rate / 100 / 12
    months = loan_years * 12

    # Monthly mortgage payment formula
    monthly_payment = (loan_amount * monthly_interest_rate) / (1 - (1 + monthly_interest_rate) ** -months)
    total_mortgage_cost = monthly_payment * months

    # Additional costs
    total_tax = tax * loan_years
    total_maintenance = maintenance * loan_years

    return total_mortgage_cost + total_tax + total_maintenance

# Streamlit app
st.title("Rent vs. Buy Calculator")
st.write("Compare the cost of renting vs. buying a property to make an informed decision.")

# User inputs
st.header("Renting Details")
rent = st.number_input("Monthly Rent ($)", value=1500.0, step=100.0)
annual_increase = st.number_input("Annual Rent Increase (%)", value=3.0, step=0.1)
years_renting = st.number_input("Number of Years Renting", value=5, step=1, min_value=1)

st.header("Buying Details")
property_price = st.number_input("Property Price ($)", value=300000.0, step=5000.0)
down_payment_percentage = st.number_input("Down Payment (%)", value=20.0, step=1.0)
mortgage_rate = st.number_input("Mortgage Interest Rate (%)", value=5.0, step=0.1)
loan_years = st.number_input("Loan Tenure (Years)", value=30, step=1, min_value=1)
tax = st.number_input("Annual Property Tax ($)", value=3000.0, step=100.0)
maintenance = st.number_input("Annual Maintenance Costs ($)", value=2000.0, step=100.0)

# Calculate costs
if st.button("Calculate"):
    renting_cost = calculate_renting_cost(rent, annual_increase, years_renting)
    buying_cost = calculate_buying_cost(property_price, down_payment_percentage, mortgage_rate, loan_years, tax, maintenance)

    st.header("Results")
    st.write(f"**Total Renting Cost:** ${renting_cost:,.2f}")
    st.write(f"**Total Buying Cost (including taxes and maintenance):** ${buying_cost:,.2f}")

    if buying_cost < renting_cost:
        st.success("Buying is more cost-effective than renting based on the inputs provided.")
    else:
        st.warning("Renting is more cost-effective than buying based on the inputs provided.")

# Add footer
st.write("\n---")
st.write("App created by Mr.ZAIN!")