Learn how to simplify and reuse concession revenue predictions by encapsulating calculations within Python functions. Enhance readability and maintainability in your code through strategic function implementation.
Key Insights
- Implementing a Python function named
predict_concessions
streamlines concession revenue predictions, making it easy to estimate, for example, approximately $70,000 for an attendance of 25,000 fans. - Using descriptive function names like
predict_concessions
enhances readability, clearly indicating the function's purpose and improving code clarity. - Encapsulating calculations within functions allows centralized modifications; changing the rounding method to integers in just one location automatically updates all predictions moving forward.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This calculation up here is the calculation we want, but we ultimately are going to want that to be something we reuse. So to do that, we're going to put it into a Python function. And this is a very good idea, and you'll see why for a number of reasons.
We're going to define a function called `predict_concessions`, and it takes in attendance. Since that's already a variable, to avoid ambiguity, we'll call this one `days_attendance`, like so. Okay.
To predict concessions from one day's attendance, we're going to return our m value times one day's attendance plus b. Okay. Let's see if that works. If we have 25,000 fans—if the ballpark comes to us and says, “Hey, DataWiz, we're expecting 25,000 fans today, how much concessions revenue can we expect?”—we say, “Okay, I've got a function for that.”
I'm going to run `predict_concessions(2,000)`. What we get back is—oh, about 70,000. And yeah, that looks about right.
So what it’s returning is: given that X, where on the line is it going to be? On average, what can we expect the concessions to be? And we can expect it to be right around there. That's our estimate.
That's our prediction. What about for another value? I’m going to make this into an official print statement. Print: “Concessions revenue for 25,000.”
Is that? Yeah. Great. Let's try another number.
Concessions revenue for—what was the other big gap here? We haven't seen a number around 28,000. We’ll run `predict_concessions(2,000)`.
Get that value. All right. So here are some of the reasons to make a little function like this.
Even though it is a fairly simple calculation as math formulas go. And the reason is twofold.
One, we can use a more human-readable name. Hey, if I run `predict_concessions` and give it an attendance, it will predict the concessions for me. It's very clear when we write this code what it is we're doing.
The other thing is that we can now work on this in one place. If we want to change how we predict concessions, it’s encapsulated in one area, as the term goes. If we look at this—this is ugly. That is an ugly amount.
Not only does the ballpark probably not care how many tens of thousands or hundreds of thousands of a cent we can expect, but they probably don't care about cents at all. In fact, we might even consider rounding it to a whole number.
But let's at least make it an integer. Let's round it to the nearest integer. And we'll do that by, before we return this value, this calculation, we'll run it through the `int()` function.
Then return what that returns. Now we’ve changed it in one place. And anytime we run `predict_concessions`, it will give it to us rounded to the nearest dollar.
So as we continue to use `predict_concessions` in this lesson, we'll see the advantage of having made that change in one place—not having to continue to run an increasingly customized calculation as we change it and having to update it in countless places. Right now what we have is a linear regression-based prediction of how much to expect in concessions given an attendance.