forked from mrayanasim09/python-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mortgage.py
49 lines (38 loc) · 1.5 KB
/
Mortgage.py
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
# This code is made by MRayan Asim
def calculate_monthly_mortgage(initial_amount, annual_interest_rate, number_of_months):
monthly_interest_rate = annual_interest_rate / (
12 * 100
) # Convert annual rate to monthly rate
monthly_payments = (
initial_amount
* (monthly_interest_rate * (1 + monthly_interest_rate) ** number_of_months)
/ ((1 + monthly_interest_rate) ** number_of_months - 1)
)
return monthly_payments
def get_float_input(prompt):
while True:
try:
value = float(input(prompt))
return value
except ValueError:
print("Invalid input. Please enter a valid number.")
def get_int_input(prompt):
while True:
try:
value = int(input(prompt))
return value
except ValueError:
print("Invalid input. Please enter a valid integer.")
def main():
print("Welcome to the Mortgage Calculator!")
print("-----------------------------------")
initial_amount = get_float_input("Enter the loan amount ($): ")
annual_interest_rate = get_float_input("Enter the annual interest rate (%): ")
number_of_months = get_int_input("Enter the loan term in months: ")
monthly_payment = calculate_monthly_mortgage(
initial_amount, annual_interest_rate, number_of_months
)
print("\nResult:")
print(f"The monthly mortgage payment is: ${monthly_payment:.2f}")
if __name__ == "__main__":
main()