Programming is similar to learning a new language--you start with basic phrases before adding together a string of words or phrases. Grammar and syntax both provide rules for structuring language, yet differing in their scope and application. This is just a starting point in exploring functions to gain a deeper understanding of python and data analytics. The following includes a basic function, and gradually I introduce layering features to increase the complexity, and hopefully, the modularity and usability of the function.
Writing a function for calculating the expected value of a home when the base cost, and multipliers are defined (# of bedrooms * bedroom multiplier cost) & (# of bathrooms * bathroom multiplier cost).
The function is simple but useless and not so user-friendly. The output simply states: 370000. There is also no user input allowed. The number of beds and baths must be changed in the code to yield a new output.
The new function incorporates a few changes that improve the overall utility. The first is the introduction of a docstring using ''' to define the purpose of the function. Next are the defined variables for ease of change. This feature is still not user-friendly, and not inclusive or a greater set of data as the variables are locked to one value. In the usage, int(input()) now allows user input to determine the number of beds and baths before giving the output. The output is now better explained using an f-string and the values from the user input before giving an expected value output in dollars.
Here is the code in action:
The last complication I want to add is a shift from static variables to dynamic variables. Realistically, the value of a house will change based on several different factors. One of these being the zip code. To implement this, I need to create a JSON structure that lists all the zip codes, and a function for user input that will only allow defined zip codes. The JSON structure for zip code ranges was parsed out with
jsonlint, a free website for validating JavaScript object notation. In my example, the new base value is arbitrary, however understanding this step will allow future real values to be implemented to create a more applicable predictor.
Use the
GitHub link to download and try out the programs for yourself.
Enjoy!
Comments
Post a Comment