0% found this document useful (0 votes)
34 views4 pages

Lucascode

This document contains a Python script that calculates whether it is better to rent or buy a home based on input parameters like home price, rent rate, length of stay, and mortgage details. It defines functions to format numbers, prompt the user, and perform calculations. The script calculates costs of buying and renting over the input length of stay, and identifies whether buying or renting is cheaper based on the total costs.

Uploaded by

api-277967114
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views4 pages

Lucascode

This document contains a Python script that calculates whether it is better to rent or buy a home based on input parameters like home price, rent rate, length of stay, and mortgage details. It defines functions to format numbers, prompt the user, and perform calculations. The script calculates costs of buying and renting over the input length of stay, and identifies whether buying or renting is cheaper based on the total costs.

Uploaded by

api-277967114
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

'''

RENT VS MORTGAGE
By
Lucas
LeVieux
(
c
)
2015
Version2
'''
##FUNCTIONS
defcommas
(
x
):
#inserts commas every three places
result
=
''
whilex

>=
1000:
x
,r
=divmod
(
x
,
1000)
result
=
",%03d%s"
%
(
r
,result)
return

"%d%s"
%
(
x
,result)
defnumprompt
(
prompt
,allowneg
=
False
,integer
=
False
,percent
=
False
):
#prompts the user,
and validates. Extra arguments for type of answer required
while

True:
result
=raw_input
(
prompt
).
lower
()
deleteitems
=
{
" "
,
"years"
,
"year"
,
","
,
"$"
,
"dollars"
,
"usd"}
fordeleteitem

indeleteitems:
result
=result
.
replace
(
deleteitem
,
"")
result
=result
.
replace
(
"k"
,
"000")
result
=result
.
replace
(
"mil"
,
"000000")
result
=result
.
replace
(
"million"
,
"000000")
result
=result
.
replace
(
"bil"
,
"000000000")
result
=result
.
replace
(
"billion"
,
"000000000")
ifresult

.
find
(
"%"
)
!=
-
1
:
#figure out if it's a percent
result
=result
.
replace
(
"%"
,
""
,
1
)
#strip it of it's percent sign
percent
=
True
#mark it for division after it's converted
ifinteger

==
False:
try:

result
=
float
(
result
)
#do the conversion
except

:
#if the conversion fails
print

"\nINPUT ERROR: %s is not a valid number"


%str
(
result)
continue

else:

try:

result
=
int
(
result
)
#do the conversion
except

:
#if the conversion fails
print

"\nINPUT ERROR: %s is not a valid integer"


%str
(
result)

continue

ifresult

<
0
andallowneg
==
False:
print

"\nINPUT ERROR: %s is a negative number"


%str
(
result)
continue

else:

break

ifpercent

==
True:
result
=result
/
100
#print result

returnresult

defpretty
(
x
):
returnstr

(
commas
(
round
(
x
,
2
)))
##PROMPTS
if
True
:
#turn to False for testing
print

"Rent or Buy? Find out with this calculator.\n"


homePrice
=numprompt
(
"Home Price: $")
rentRate
=numprompt
(
"Rent Rate per Month: $")
stayLength
=numprompt
(
"Length of Stay (years): "
,
False
,
True
)
*
12
#years to months
mortgagePayment
=numprompt
(
"\nMortgage Payment per Month: $")
mortgageDownPayment
=numprompt
(
"Down Payment (percent): "
,
False
,
False
,
True)
else:
homePrice
=
float
(
250000)
rentRate
=
float
(
906)
stayLength
=
int
(
9
*
12)
mortgagePayment
=
float
(
939)
mortgageDownPayment
=
float
(.
2)
##CONSTANTS
#Initial
mortgageLength
=
30
buyingFees
=
.
04
*homePrice
sellingFeesPercent
=
.
06
#Monthly
propertyTax
=
.
0073
/
12
rentInsurance
=
.
0132
*rentRate
homeownersInsurance
=
.
0046
/
12
*homePrice
renovations
=
(.
01
*homePrice
)
/
12
extraHomeownerUtilityCost
=
100

priceAppreciationRate
=
.
0281
/
12
inflation
=
.
02
/
12
##CALCULATIONS
Buy
=
{}
Rent
=
{}
#Mortgage calculations
Buy
[
"initialCost"
]
=
(
mortgageDownPayment
*homePrice
)
+buyingFees
Buy
[
"monthlyCost"
]
=mortgagePayment
+
(
propertyTax
*homePrice
)
+homeownersInsurance
+
renovations
+extraHomeownerUtilityCost
Buy
[
"recurringCosts"
]
=0
formonth
inrange
(
0
,stayLength
):
#month by month calculations for buy
homePrice
=homePrice
+
((
priceAppreciationRate
+inflation
)
*homePrice)
Buy

[
"recurringCosts"
]
=
Buy
[
"recurringCosts"
]
+mortgagePayment
+
(
propertyTax
*homePrice
)
+homeownersInsurance
+renovations
+extraHomeownerUtilityCost
Buy
[
"sale"
]
=homePrice
/
2
-
((
mortgageLength
-stayLength
/
12
)
/mortgageLength
*
(
homePrice
-mortgageDownPayment
*homePrice
))
-sellingFeesPercent
*homePrice
Buy
[
"total"
]
=
Buy
[
"initialCost"
]
+
Buy
[
"recurringCosts"
]
-
Buy
[
"sale"]
#Rent calculations
Rent
[
"initialCost"
]
=rentRate
+rentInsurance
Rent
[
"monthlyCost"
]
=rentRate
+rentInsurance
Rent
[
"total"
]
=
Rent
[
"initialCost"]
formonth
inrange
(
0
,stayLength
):
#month by month calculations for rent
rentRate
=rentRate
+
(
inflation
*rentRate)
Rent

[
"total"
]
=
Rent
[
"total"
]
+rentRate
+rentInsurance
#Comparisons
if
Rent
[
"total"
]
<
Buy
[
"total"
]:
winner
=
"Renting"
margin
=
(
Buy
[
"total"
]-
Rent
[
"total"
])/
Rent
[
"total"]
else:
winner
=
"Buying"
margin
=
(
Rent
[
"total"
]-
Buy
[
"total"
])/
Buy
[
"total"]
##RESULTS
print
"\n\nThe results are in!\n"
print
"BUYING\nInitial Cost: $"
+pretty
(
Buy
[
"initialCost"
])
print
"Initial Monthly Cost: $"
+pretty
(
Buy
[
"monthlyCost"
])
print
"Total Monthly Cost: $"
+pretty
(
Buy
[
"recurringCosts"
])
print
"Sale proceeds: $"
+pretty
(
Buy
[
"sale"
])
print
"Total Cost: $"
+pretty
(
Buy
[
"total"
])

print
"\nRENTING\nInitial Cost: $"
+pretty
(
Rent
[
"initialCost"
])
print
"Initial Monthly Cost: $"
+pretty
(
Rent
[
"monthlyCost"
])
print
"Total Cost: $"
+pretty
(
Rent
[
"total"
])
print
"\nWINNER: "
+winner
+
"!\nby "
+str
(
round
(
margin
*
100
,
2
))
+
"%"

You might also like