Lucascode
Lucascode
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
else:
try:
result
=
int
(
result
)
#do the conversion
except
:
#if the conversion fails
print
continue
ifresult
<
0
andallowneg
==
False:
print
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
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
))
+
"%"