Solution: House Hunting
This article is Part 2 of a Python programming challenge. In part 1, we looked at what would be needed for a program to answer the question "How long (in months) would you have to save in order to afford a down payment given the cost of the home, a salary, and the portion to be saved each month?" In this article we'll look at one potential solution.
Solution
I chose to implement my program within Jupyter, but the same approach would work as a command line script. I've organized the logic into three main sections:
- Prompt for user input and convert the data to numerical types that can be used for computation.
- Extract other information from the input and store those as intermediate variables.
- Calculate the answer and output the result.
Code
# Set initial state variables portion_down_payment = 0.25 # Percent of home value for down payment r = 0.04 # Rate of investment return # Input salary, portion saved, and total cost of home total_cost = float(input('How much is the home: ')) annual_salary = float(input('Please enter the annual salary of the job: ')) portion_saved = float(input('Enter the amount to be saved each month as ratio (example: 0.1 for 10%)')) # Monthly salary and down payment monthly_salary = annual_salary/12 monthly_return = r/12 down_payment = portion_down_payment*total_cost # Months and current savings months = 0 current_savings = 0 while current_savings <= down_payment: # Increase current savings using savings and return on investment current_savings += portion_saved*monthly_salary + current_savings*monthly_return months += 1 print('%s months required to save %s down payment for a %s dollary home with a salary of %s setting aside %s' % (months, down_payment, total_cost, annual_salary, portion_saved))
Capturing User Input
There are three pieces of information that I need: the total cost of the house, the annual salary of the job, and the amount each month to set aside (as a fraction of the monthly salary). I use Python's input
keyword to prompt for that information.
When run from within Jupyter, input
creates a text box and pauses execution of the program until the user provides information. When run from a terminal or script, it will use a line-based input.
The data provided via input
will be text, so the next thing I do is convert that to a numerical value by passing the input to float
. This is not the most robust way to manage the conversion, since values that cannot be directly converted to numbers via float
such as "1,000,000" or "Hello!" will cause the program to crash.
In a "real" program, ensuring that data is "valid" before trying to work with it is very important. Python has several good libraries that can be used to create "Validators" or "Forms" including Cerberus and WTForms. Such libraries allow you to declare data structure and provide ways to convert, "clean", and verify it before moving into the logic of the program.
Intermediate Variables
After converting the cost of the home, the annual salary, and portion of the salary to save; the next part of the program creates intermediate variables that will be used in computation.
- The program will solve for the number of months needed to save for a down payment. Because of that, it's more convenient to think about most of the input variables within the context of a month. For this reason, I chose to convert the annual salary to a monthly salary and the yearly return to a monthly return by dividing by twelve. I ask the user to provide yearly values because salary and investment returns are normally thought of about in terms of years.
- I also calculate the target amount for the down payment by multiplying the desired proportion (a hard coded variable), and the total cost entered by the user. The down payment amount is important to the flow of the program because it is used to exit the loop.
- The last two variables we create are the count of months (
months
) and the savings (current_savings
). These values are "state" variables that will be used by the program to keep track of how the savings grow over time and the number of months required to reach the savings goal.
Calculating the Number of Months
The amount of the down payment grows from two contributions: a constant amount that is taken from the monthly salary (portion_saved*monthly_salary
) and a compounded amount that comes from the return of the previous month's total savings (current_savings*monthly_return
).
Because we don't know how long will be needed to accrue the money needed for the down payment, I chose to execute the logic of the program inside of a while
loop. Each pass through the loop, the value of the current savings is compared to the amount needed for the down payment. When the savings value equals or exceeds the down payment, it will exit the loop. Inside of the loop, I calculate the increase in the savings for the month and increment the count.
Reporting the Results
Once the loop exits, the final piece of the program reports the results. It uses the print
function and a template string to write the number of months (along with the input factors) to standard out.
Comments
Loading
No results found