Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Create a reusable function to remove calculations
-
0:00
This is looking great and Monty Python thinks so, too.
-
0:03
I showed a demo to the group and
-
0:05
they realized that they forgot to include a requirement.
-
0:08
They forgot that there's a service charge involved with each transaction.
-
0:11
You can't sell tickets without a service charge, right?
-
0:15
Now this works a little differently.
-
0:17
Each purchase, not each ticket, has a service charge of $2.
-
0:22
Every time you demo your software, users will request additional features.
-
0:25
It happens all the time.
-
0:27
This is why it's important to get working software in front of your stakeholders.
-
0:31
This is actually a good chance to take a look at our code and
-
0:34
see if we can't refactor it a bit to make it more easy to read.
-
0:38
Well, there's a term we haven't touched on yet, refactor.
-
0:41
Refactoring is when you take a look at your code and you improve it for
-
0:44
readability or extensibility without changing how the program actually works.
-
0:48
Let's see if we can't refactor that price calculation into a function and
-
0:52
then add this new service charge.
-
0:54
Okay, so I'm gonna go ahead.
-
0:55
And I'm gonna add a new card.
-
0:57
And this one is As an owner, I should receive,
-
1:05
A service charge so that I can pay
-
1:10
others to maintain the software.
-
1:15
I suppose that makes sense, right?
-
1:17
Maintaining software can be super difficult for clients.
-
1:19
Now, if there's an error, they'll need to pay developers somehow.
-
1:23
So, I hope some of that service charge makes its way to fellow developers,
-
1:28
cuz it might not be us that fixes it.
-
1:29
You might pick up an application that's already working and you need to fix it.
-
1:33
So, let's move this into In Progress.
-
1:36
Okay, so, I'm gonna get rid of these comments here, get rid of that one, and
-
1:41
that one, and that one.
-
1:44
Okay, looking good, all right.
-
1:48
So let's first refactor our calculation into a function, right?
-
1:54
Cuz currently, we are calculating.
-
1:56
Where are we doing that calculation?
-
1:58
Right here, num_tickets equals times TICKET_PRICE.
-
2:01
So let's go ahead, I'm gonna cut this out.
-
2:04
This is Cmd+X, or Ctrl+X.
-
2:06
So now it's in my clipboard, it's gone.
-
2:08
And I'm gonna add a function that we'll create here in a bit.
-
2:12
And it should calculate the price of how many tickets there are.
-
2:15
So sounds like a good name, calculate_price.
-
2:20
And we're gonna pass in the number of tickets,
-
2:24
which we know is a valid number at this point.
-
2:28
Go ahead and save that.
-
2:29
And I'm gonna up here to the top, and here we go, let's do this.
-
2:33
Create the calculate price function.
-
2:37
Let's use the proper name there, calculate_price function.
-
2:44
It takes Number of tickets and
-
2:49
returns, what do we have here?
-
2:52
num_tickets + TICKET_PRICE, let's make a new.
-
2:57
Cool, so create that function, and return that value.
-
3:02
All right, you got this.
-
3:03
Pause me and create that function.
-
3:05
Remember, it needs to take the number of tickets.
-
3:09
All right, so here's what I did.
-
3:10
So I defined calculate_price.
-
3:13
And I required a parameter of number of tickets.
-
3:17
I need a colon, open that body up, and there is no need to create a new variable.
-
3:22
We can actually just return the result, right?
-
3:24
So we're gonna return and let's get lazy.
-
3:28
This, paste this here, but note that this is number of tickets.
-
3:33
So I'm gonna say number_of_tickets.
-
3:38
Now, note how this refactoring puts this calculating price into a separate area
-
3:43
than where this loop is at there.
-
3:45
So this loop will never really need to change.
-
3:48
We can figure out what this calculation of the price is.
-
3:51
And other people could use it too, should they need to.
-
3:54
We didn't need to do this but we refactored and
-
3:57
things should still work exactly the same.
-
3:59
Let's go ahead and let's run it and make sure.
-
4:03
Hey, Bob, let's get 2 tickets.
-
4:05
We got 20.
-
4:05
Yes, I wanna proceed.
-
4:07
98 tickets left, awesome, perfect.
-
4:09
So now that we have it refactored,
-
4:13
what we should do is we need to add this service charge, right?
-
4:17
So I'm just gonna go ahead, I'll put it in here.
-
4:22
And we need to create a new constant for
-
4:26
the $2 service charge.
-
4:31
Remember, that's once per transaction.
-
4:35
And then we want to add the service charge to what's due.
-
4:44
Okay, you got this.
-
4:45
Pause me and give those a go.
-
4:47
You ready?
-
4:49
Okay, so here's how I did it.
-
4:51
So this service charge, I'm gonna go ahead, I'm gonna come up here.
-
4:53
I'm gonna make a new constant.
-
4:57
And I'm gonna put it at the top of the file.
-
4:59
If you ever look in here, the service charge, if we start charging too much for
-
5:02
our developers, we need to bump this price up.
-
5:05
We just bump it one place here.
-
5:07
And then, I used it in the calculate_price function.
-
5:11
So I'm gonna get rid of this comment here, bring this back up.
-
5:16
We can just say + SERVICE_CHARGE.
-
5:21
You know what, I'm gonna think about my dear Aunt Sally.
-
5:25
And I'm gonna use some parenthesis even though I don't need to.
-
5:29
Because I know that multiplication will happen first and not the addition.
-
5:33
But I'm gonna do that cuz I think that that makes things more clear.
-
5:37
Let's go ahead and see how we did.
-
5:42
I would like to have 2 tickets.
-
5:44
$22, because of that service charge.
-
5:48
And there we go, great job.
-
5:51
Now I do like how if they change the way that this works,
-
5:55
we know where to change things.
-
5:56
It's right up here at the calculate_price.
-
5:58
And when you look at it used in this loop here, it's pretty clean, right?
-
6:03
It's really clear that the price calculation is happening elsewhere, and
-
6:06
we don't need to worry about it here.
-
6:08
If you wanted to calculate this on a different page,
-
6:10
on like a shopping cart page, you could use that same function.
-
6:13
It's reusable, we change it in one place.
-
6:15
And you know what?
-
6:17
I think we're done.
-
6:19
Awesome job.
-
6:21
I want you to take a minute and breathe in this program.
-
6:25
Look at all the tools that you stitched together.
-
6:29
You really have learned a ton and you were able to build an entire application.
-
6:34
You did an excellent job at immersing yourself in the Python
-
6:36
programming language.
-
6:38
Excellent work.
You need to sign up for Treehouse in order to download course files.
Sign up