WEBVTT

1
00:00:00.571 --> 00:00:03.720
This is looking great and
Monty Python thinks so, too.

2
00:00:03.720 --> 00:00:05.040
I showed a demo to the group and

3
00:00:05.040 --> 00:00:08.300
they realized that they forgot
to include a requirement.

4
00:00:08.300 --> 00:00:11.403
They forgot that there's a service
charge involved with each transaction.

5
00:00:11.403 --> 00:00:14.060
You can't sell tickets without
a service charge, right?

6
00:00:15.490 --> 00:00:17.070
Now this works a little differently.

7
00:00:17.070 --> 00:00:22.270
Each purchase, not each ticket,
has a service charge of $2.

8
00:00:22.270 --> 00:00:25.830
Every time you demo your software,
users will request additional features.

9
00:00:25.830 --> 00:00:27.530
It happens all the time.

10
00:00:27.530 --> 00:00:31.880
This is why it's important to get working
software in front of your stakeholders.

11
00:00:31.880 --> 00:00:34.650
This is actually a good chance
to take a look at our code and

12
00:00:34.650 --> 00:00:38.170
see if we can't refactor it a bit
to make it more easy to read.

13
00:00:38.170 --> 00:00:41.170
Well, there's a term we haven't
touched on yet, refactor.

14
00:00:41.170 --> 00:00:44.100
Refactoring is when you take a look
at your code and you improve it for

15
00:00:44.100 --> 00:00:48.970
readability or extensibility without
changing how the program actually works.

16
00:00:48.970 --> 00:00:52.290
Let's see if we can't refactor that
price calculation into a function and

17
00:00:52.290 --> 00:00:53.660
then add this new service charge.

18
00:00:54.780 --> 00:00:55.960
Okay, so I'm gonna go ahead.

19
00:00:55.960 --> 00:00:57.420
And I'm gonna add a new card.

20
00:00:57.420 --> 00:01:02.946
And this one is As an owner,
I should receive,

21
00:01:05.644 --> 00:01:10.278
A service charge so that I can pay

22
00:01:10.278 --> 00:01:15.270
others to maintain the software.

23
00:01:15.270 --> 00:01:17.100
I suppose that makes sense, right?

24
00:01:17.100 --> 00:01:19.740
Maintaining software can be
super difficult for clients.

25
00:01:19.740 --> 00:01:23.470
Now, if there's an error,
they'll need to pay developers somehow.

26
00:01:23.470 --> 00:01:28.120
So, I hope some of that service charge
makes its way to fellow developers,

27
00:01:28.120 --> 00:01:29.730
cuz it might not be us that fixes it.

28
00:01:29.730 --> 00:01:33.540
You might pick up an application that's
already working and you need to fix it.

29
00:01:33.540 --> 00:01:35.890
So, let's move this into In Progress.

30
00:01:36.930 --> 00:01:41.596
Okay, so, I'm gonna get rid of these
comments here, get rid of that one, and

31
00:01:41.596 --> 00:01:43.101
that one, and that one.

32
00:01:44.878 --> 00:01:46.446
Okay, looking good, all right.

33
00:01:48.671 --> 00:01:54.630
So let's first refactor our
calculation into a function, right?

34
00:01:54.630 --> 00:01:56.810
Cuz currently, we are calculating.

35
00:01:56.810 --> 00:01:58.380
Where are we doing that calculation?

36
00:01:58.380 --> 00:02:01.640
Right here,
num_tickets equals times TICKET_PRICE.

37
00:02:01.640 --> 00:02:04.240
So let's go ahead, I'm gonna cut this out.

38
00:02:04.240 --> 00:02:06.370
This is Cmd+X, or Ctrl+X.

39
00:02:06.370 --> 00:02:07.720
So now it's in my clipboard, it's gone.

40
00:02:08.920 --> 00:02:12.050
And I'm gonna add a function
that we'll create here in a bit.

41
00:02:12.050 --> 00:02:15.660
And it should calculate the price
of how many tickets there are.

42
00:02:15.660 --> 00:02:20.560
So sounds like a good name,
calculate_price.

43
00:02:20.560 --> 00:02:24.670
And we're gonna pass in
the number of tickets,

44
00:02:24.670 --> 00:02:26.960
which we know is a valid
number at this point.

45
00:02:28.250 --> 00:02:29.810
Go ahead and save that.

46
00:02:29.810 --> 00:02:33.978
And I'm gonna up here to the top,
and here we go, let's do this.

47
00:02:33.978 --> 00:02:37.584
Create the calculate price function.

48
00:02:37.584 --> 00:02:44.348
Let's use the proper name there,
calculate_price function.

49
00:02:44.348 --> 00:02:49.057
It takes Number of tickets and

50
00:02:49.057 --> 00:02:52.986
returns, what do we have here?

51
00:02:52.986 --> 00:02:55.830
num_tickets + TICKET_PRICE,
let's make a new.

52
00:02:57.060 --> 00:03:02.170
Cool, so create that function,
and return that value.

53
00:03:02.170 --> 00:03:03.400
All right, you got this.

54
00:03:03.400 --> 00:03:05.020
Pause me and create that function.

55
00:03:05.020 --> 00:03:07.450
Remember, it needs to take
the number of tickets.

56
00:03:09.120 --> 00:03:10.540
All right, so here's what I did.

57
00:03:10.540 --> 00:03:13.305
So I defined calculate_price.

58
00:03:13.305 --> 00:03:17.495
And I required a parameter
of number of tickets.

59
00:03:17.495 --> 00:03:22.975
I need a colon, open that body up, and
there is no need to create a new variable.

60
00:03:22.975 --> 00:03:24.665
We can actually just return the result,
right?

61
00:03:24.665 --> 00:03:28.335
So we're gonna return and let's get lazy.

62
00:03:28.335 --> 00:03:33.245
This, paste this here, but
note that this is number of tickets.

63
00:03:33.245 --> 00:03:36.422
So I'm gonna say number_of_tickets.

64
00:03:38.786 --> 00:03:43.323
Now, note how this refactoring puts this
calculating price into a separate area

65
00:03:43.323 --> 00:03:45.600
than where this loop is at there.

66
00:03:45.600 --> 00:03:48.410
So this loop will never
really need to change.

67
00:03:48.410 --> 00:03:51.030
We can figure out what this
calculation of the price is.

68
00:03:51.030 --> 00:03:53.380
And other people could use it too,
should they need to.

69
00:03:54.540 --> 00:03:57.380
We didn't need to do this but
we refactored and

70
00:03:57.380 --> 00:03:59.580
things should still work exactly the same.

71
00:03:59.580 --> 00:04:00.634
Let's go ahead and
let's run it and make sure.

72
00:04:03.577 --> 00:04:05.410
Hey, Bob, let's get 2 tickets.

73
00:04:05.410 --> 00:04:05.985
We got 20.

74
00:04:05.985 --> 00:04:07.200
Yes, I wanna proceed.

75
00:04:07.200 --> 00:04:09.640
98 tickets left, awesome, perfect.

76
00:04:09.640 --> 00:04:13.020
So now that we have it refactored,

77
00:04:13.020 --> 00:04:17.370
what we should do is we need to
add this service charge, right?

78
00:04:17.370 --> 00:04:22.520
So I'm just gonna go ahead,
I'll put it in here.

79
00:04:22.520 --> 00:04:26.948
And we need to create a new constant for

80
00:04:26.948 --> 00:04:31.030
the $2 service charge.

81
00:04:31.030 --> 00:04:35.810
Remember, that's once per transaction.

82
00:04:35.810 --> 00:04:39.393
And then we want to add
the service charge to what's due.

83
00:04:44.254 --> 00:04:45.910
Okay, you got this.

84
00:04:45.910 --> 00:04:47.620
Pause me and give those a go.

85
00:04:47.620 --> 00:04:48.120
You ready?

86
00:04:49.570 --> 00:04:51.040
Okay, so here's how I did it.

87
00:04:51.040 --> 00:04:53.700
So this service charge, I'm gonna
go ahead, I'm gonna come up here.

88
00:04:53.700 --> 00:04:57.523
I'm gonna make a new constant.

89
00:04:57.523 --> 00:04:59.390
And I'm gonna put it at
the top of the file.

90
00:04:59.390 --> 00:05:02.870
If you ever look in here, the service
charge, if we start charging too much for

91
00:05:02.870 --> 00:05:05.250
our developers,
we need to bump this price up.

92
00:05:05.250 --> 00:05:07.450
We just bump it one place here.

93
00:05:07.450 --> 00:05:11.870
And then,
I used it in the calculate_price function.

94
00:05:11.870 --> 00:05:14.910
So I'm gonna get rid of this comment here,
bring this back up.

95
00:05:16.900 --> 00:05:21.772
We can just say + SERVICE_CHARGE.

96
00:05:21.772 --> 00:05:25.348
You know what,
I'm gonna think about my dear Aunt Sally.

97
00:05:25.348 --> 00:05:28.690
And I'm gonna use some parenthesis
even though I don't need to.

98
00:05:29.830 --> 00:05:33.330
Because I know that multiplication will
happen first and not the addition.

99
00:05:33.330 --> 00:05:36.010
But I'm gonna do that cuz I think
that that makes things more clear.

100
00:05:37.490 --> 00:05:39.394
Let's go ahead and see how we did.

101
00:05:42.001 --> 00:05:44.076
I would like to have 2 tickets.

102
00:05:44.076 --> 00:05:48.040
$22, because of that service charge.

103
00:05:48.040 --> 00:05:50.780
And there we go, great job.

104
00:05:51.980 --> 00:05:55.190
Now I do like how if they
change the way that this works,

105
00:05:55.190 --> 00:05:56.920
we know where to change things.

106
00:05:56.920 --> 00:05:58.656
It's right up here at the calculate_price.

107
00:05:58.656 --> 00:06:03.196
And when you look at it used in this
loop here, it's pretty clean, right?

108
00:06:03.196 --> 00:06:06.524
It's really clear that the price
calculation is happening elsewhere, and

109
00:06:06.524 --> 00:06:08.196
we don't need to worry about it here.

110
00:06:08.196 --> 00:06:10.497
If you wanted to calculate
this on a different page,

111
00:06:10.497 --> 00:06:13.380
on like a shopping cart page,
you could use that same function.

112
00:06:13.380 --> 00:06:15.820
It's reusable, we change it in one place.

113
00:06:15.820 --> 00:06:17.810
And you know what?

114
00:06:17.810 --> 00:06:19.940
I think we're done.

115
00:06:19.940 --> 00:06:21.670
Awesome job.

116
00:06:21.670 --> 00:06:25.910
I want you to take a minute and
breathe in this program.

117
00:06:25.910 --> 00:06:29.780
Look at all the tools that
you stitched together.

118
00:06:29.780 --> 00:06:34.260
You really have learned a ton and you
were able to build an entire application.

119
00:06:34.260 --> 00:06:36.950
You did an excellent job at
immersing yourself in the Python

120
00:06:36.950 --> 00:06:38.370
programming language.

121
00:06:38.370 --> 00:06:39.780
Excellent work.
