WEBVTT

0
00:00.050 --> 00:03.590
Hey guys, welcome to the final project of today.

1
00:03.590 --> 00:08.930
And in this final project, we're going to be building a Tip Calculator.

2
00:08.930 --> 00:11.750
So the final output is going to look like this.

3
00:11.780 --> 00:12.710
It's going to print,

4
00:12.710 --> 00:14.420
"Welcome to the tip calculator."

5
00:14.420 --> 00:19.490
And it's going to ask you for an input for how much the total bill came to.

6
00:19.520 --> 00:30.740
Let's say that we and a couple of friends went out for lunch, and it came to $124.56, and then it'll

7
00:30.770 --> 00:33.470
ask you, "What percentage tip would you like to give?"

8
00:33.500 --> 00:36.410
So let's say we want to give a 12% tip.

9
00:36.410 --> 00:38.750
So we enter 12 and hit Enter.

10
00:38.780 --> 00:42.560
And then finally it asks, "How many people are splitting this bill?"

11
00:42.560 --> 00:45.800
Let's say there's seven of us and then hit Enter.

12
00:45.830 --> 00:51.530
Finally, it's going to tell us that each person should pay about $19.93.

13
00:51.860 --> 00:58.130
Notice how the final bill, even though after all of these calculations, it's probably got more numbers

14
00:58.130 --> 01:05.690
after the decimal point, we only want two decimal places of accuracy, so it should be rounded to two

15
01:05.690 --> 01:06.890
decimal places.

16
01:06.920 --> 01:14.150
The second thing to remember is that these are percentages, and in order to calculate a percentage

17
01:14.150 --> 01:20.870
of something, you can multiply a number by the percentage number divided by 100.

18
01:21.170 --> 01:26.780
Let me show you this in a little bit more detail, because this project is not about testing your maths,

19
01:26.780 --> 01:30.530
it's about seeing how well you've understood the programming concepts.

20
01:30.560 --> 01:35.570
So let's say that we had a bill of $150.

21
01:35.570 --> 01:45.890
If we were to apply a 12% tip on top of that, then 12% is going to be equal to 12 divided by 100,

22
01:45.920 --> 01:48.710
which is equal to 0.12.

23
01:49.010 --> 01:55.700
Now the next step is we can multiply 150 the total bill by 0.12.

24
01:55.730 --> 02:01.400
And this will give us what 12% of 150 is, which is 18.

25
02:01.460 --> 02:08.170
Now we of course have to add the tip onto the final bill so it becomes 150 + 18.

26
02:08.170 --> 02:13.810
So $150 with 12% tip is equal to $168.

27
02:13.840 --> 02:22.480
Now a shorthand way of doing all of this is simply multiplying 150 by 1.12.

28
02:22.510 --> 02:29.410
So the one is 150, and then it's 0.12 on top of that.

29
02:29.410 --> 02:33.550
So when we do this, it gives us pretty much the same number.

30
02:33.730 --> 02:39.070
Now if during your testing and your coding, you come across some sort of number that looks a little

31
02:39.100 --> 02:45.460
bit strange because you think 150 multiplied by 1.12 should actually equal 168

32
02:45.460 --> 02:46.540
precisely,

33
02:46.540 --> 02:49.780
so what are all of these extra numbers at the end?

34
02:49.810 --> 02:53.050
Now, the short answer is you don't have to worry about it.

35
02:53.050 --> 02:58.690
It's simply related to how Python processes these floating point numbers.

36
02:58.690 --> 03:04.930
If you're really interested and you want to read about this, then I'll link to this page in the Python

37
03:04.930 --> 03:08.380
documentation where they tell you why this is happening,

38
03:08.380 --> 03:11.440
but be warned, it's pretty dense and it's pretty heavy.

39
03:11.440 --> 03:17.650
But the final conclusion is it's just the way that Python is approximating this number, so you don't

40
03:17.680 --> 03:19.420
actually have to worry about this.

41
03:19.450 --> 03:25.150
Now, once we've gotten to 168, the next step is to split it between five people.

42
03:25.150 --> 03:31.810
So, 168 / 5 = 33.6.

43
03:31.840 --> 03:38.080
Now what we want to show the user is we want to show them the amount that they have to pay with two

44
03:38.080 --> 03:39.430
decimal places of accuracy.

45
03:39.460 --> 03:45.190
So we want to say something like 33.60 if this is the case, because that's normally how we represent

46
03:45.220 --> 03:47.710
numbers when it comes to money.

47
03:47.710 --> 03:54.520
So what I want you to do is to also be able to round any of these numbers to two decimal places.

48
03:54.970 --> 04:01.870
If you have successfully created this program, then it should work exactly as this example version

49
04:01.870 --> 04:07.410
would, and you'll be able to see the formatting, where it actually tells you each person should pay

50
04:07.410 --> 04:12.450
this particular dollar amount, and it's rounded to two decimal places.

51
04:12.450 --> 04:19.590
And this is the amount, after taking into consideration these three different inputs from the user.

52
04:19.620 --> 04:25.020
Have a play around with the final product, and then have a think about how you would create this program

53
04:25.020 --> 04:28.500
step by step, and then try to achieve this result.

54
04:28.800 --> 04:34.530
This is going to test everything that you've learned so far, including f-strings, including complex

55
04:34.530 --> 04:41.730
mathematical operations including PEMDAS and calculating numbers, and the order in which mathematical

56
04:41.730 --> 04:47.340
operations are run by a computer, as well as everything you've learned in the previous lessons, and

57
04:47.340 --> 04:48.900
previous days as well.

58
04:48.930 --> 04:53.520
I think this is going to be a lot of fun, and the end result is going to be kind of useful the next

59
04:53.520 --> 04:55.920
time that you need to split the bill between friends.

60
04:55.920 --> 04:58.470
So I'm going to let you pause the video and get on with it.

61
04:58.470 --> 05:03.360
And then once you're done, if you want to, head back over here and I'll walk through the solution with

62
05:03.360 --> 05:08.010
you, but I really recommend you giving it a good go before you come back.

63
05:08.010 --> 05:10.590
So pause the video now and I'll see you later.

64
05:15.780 --> 05:20.760
Okay, so we've seen how the program is supposed to work, and we're trying to create it from scratch

65
05:20.760 --> 05:23.400
by reverse engineering it essentially.

66
05:23.520 --> 05:27.690
Now the first thing our program should say is, "Welcome to the tip calculator!"

67
05:27.690 --> 05:29.490
So that's easy enough.

68
05:29.490 --> 05:31.710
We just have to create a print statement.

69
05:31.740 --> 05:32.580
Perfect.

70
05:32.580 --> 05:38.550
And if you're creating a really enthusiastic tip calculator you can replace the full stop with an exclamation

71
05:38.550 --> 05:39.240
mark.

72
05:39.450 --> 05:44.610
Okay, so let's run it step by step to make sure that we don't have any errors along the way.

73
05:44.610 --> 05:47.370
So the first step seems to be working.

74
05:47.400 --> 05:50.910
Now the next step is to ask the user for some input,

75
05:50.910 --> 05:53.310
and we want to know what was their total bill.

76
05:53.310 --> 05:54.570
Where did it come to?

77
05:54.600 --> 06:00.270
So let's go ahead and create an input and ask them, "What was the total bill?"

78
06:00.300 --> 06:05.910
And then we'll add a dollar sign or whatever currency it is that you prefer to work with

79
06:05.940 --> 06:12.660
because when we actually run this, you'll see that the input will go straight after the end of that

80
06:12.660 --> 06:13.050
string.

81
06:13.050 --> 06:14.820
So, add the dollar sign.

82
06:14.820 --> 06:17.820
So now we can enter a dollar amount like this,

83
06:17.820 --> 06:19.860
but it's not really saved anywhere.

84
06:19.860 --> 06:24.810
This data just disappears because we haven't stored it inside a variable.

85
06:24.810 --> 06:26.400
So let's go ahead and do that.

86
06:26.400 --> 06:28.980
Let's call this variable, bill.

87
06:28.980 --> 06:33.600
And we'll set it to equal whatever the user typed in to this input.

88
06:33.630 --> 06:41.490
Now remember that this bill is going to have a data type of a string.

89
06:41.520 --> 06:41.880
Right?

90
06:41.910 --> 06:48.600
So if I go ahead and hit run and let's just type some numbers in here, you'll see that the data type

91
06:48.600 --> 06:51.390
of the bill is, as we said, a string.

92
06:51.510 --> 06:58.350
In order to be able to do maths, you might remember we have to change this into a number format.

93
06:58.350 --> 07:00.210
So a float or an int.

94
07:00.240 --> 07:05.790
Now in this case, because the bill is likely to have numbers after the decimal place, it's probably

95
07:05.790 --> 07:11.960
better that we turn it into a float so that we get the most accurate result possible.

96
07:12.530 --> 07:19.340
Now that we're done with the bill, the next question the tip calculator should ask is what percentage

97
07:19.340 --> 07:20.720
tip would you like to give,

98
07:20.750 --> 07:22.490
10, 12, or 15?

99
07:22.490 --> 07:26.990
Or you can of course switch this up depending on how much tip you normally give.

100
07:27.050 --> 07:30.770
So the next line is going to be another input.

101
07:32.420 --> 07:36.830
Here I'm asking the user, "How much tip would you like to give?" and giving them some examples.

102
07:36.830 --> 07:43.640
And notice how in my examples, I don't actually have the percentage sign here, because I don't really

103
07:43.640 --> 07:53.150
want them to type the percent sign into this input, because if they do, when they say 12%, this is

104
07:53.150 --> 07:59.870
going to be really hard for me to turn the 12% into an actual number.

105
07:59.870 --> 08:06.080
So you could, in your input message, say something like, "How much tip would you like to give?

106
08:06.260 --> 08:07.520
10, 12 or 15?"

107
08:07.520 --> 08:12.110
Please don't add any percentage signs, just add the number or something like that.

108
08:12.200 --> 08:19.430
But again, once we've done this, it's not stored anywhere unless we add it into a variable.

109
08:19.460 --> 08:24.350
Now we've got a bill that stores the total bill as a float.

110
08:24.350 --> 08:29.750
And we've got a tip which is going to be a whole number, 10, 12 or 15.

111
08:29.780 --> 08:30.290
Right?

112
08:30.290 --> 08:36.590
So depending on what you think is best, you can either turn this into a float or in my case I'm probably

113
08:36.590 --> 08:39.740
going to convert it into an integer.

114
08:39.830 --> 08:45.140
So now we're on to the final step which is how many people to split the bill between.

115
08:45.140 --> 08:48.830
So let's go ahead and create a variable called people.

116
08:48.860 --> 08:57.290
And I'm going to add an int wrapper around this input so that even in the very beginning we know that

117
08:57.290 --> 09:00.590
we have to convert the number of people into a whole number,

118
09:00.590 --> 09:04.820
because I've never had a meal with 3.5 people before.

119
09:05.150 --> 09:09.760
So the input is going to be, "ow many people to split the bill?"

120
09:10.720 --> 09:17.380
And at this point, they should enter the number of people which will turn into an integer stored inside

121
09:17.380 --> 09:19.510
this variable called people.

122
09:19.540 --> 09:25.360
So now that we've got all the data collected from the user, we're finally ready to do some maths.

123
09:25.390 --> 09:35.410
And we said that the way to work out the tip is by multiplying it by 1 point, and then the number

124
09:35.410 --> 09:39.940
after the decimal point is whatever percentage they decided to give.

125
09:39.970 --> 09:46.480
So it would be 1.1 if it was 10% and 1.15 if it was 15%.

126
09:46.540 --> 09:51.610
Let's go ahead and calculate the bill_with_tip.

127
09:52.330 --> 10:00.940
So remember how we said that before the tip percentage is equal to the whole number 10, 12 ,or 15 divided

128
10:00.940 --> 10:06.040
by 100 which turns it into 0.1, 0.12, or 0.15,

129
10:06.070 --> 10:11.260
and then once this calculation is done, we can multiply it by the bill.

130
10:11.710 --> 10:15.850
And then finally we add that to the original bill.

131
10:16.420 --> 10:22.450
So let's go ahead and print this and just check to see that it looks sensible.

132
10:22.450 --> 10:25.900
So bill_with_tip is what we're going to print.

133
10:25.900 --> 10:29.050
And let's put some easy numbers in there.

134
10:29.350 --> 10:34.720
Let's say that the bill was $100 and the tip was 10%,

135
10:34.720 --> 10:38.530
so then the 10% of $100 is $10.

136
10:38.530 --> 10:40.300
So the bill_with_tip,

137
10:40.300 --> 10:44.230
that's what's going to be printed next, it should be 110.

138
10:44.230 --> 10:47.110
So we can type anything in here because we're not using it,

139
10:47.110 --> 10:52.450
and you can see that this calculation, bill_with_tip is 110, which is what we want.

140
10:52.480 --> 10:56.200
Now there's a lot of other ways that you can express this.

141
10:56.200 --> 11:04.630
For example, you could say bill * (1 + tip) / 100).

142
11:04.630 --> 11:08.560
So this would actually give you the same result as well.

143
11:08.560 --> 11:14.950
But remember what we said previously about how Python is a little bit weird with floating point numbers,

144
11:14.950 --> 11:19.510
but essentially this is still giving us the same result, which is 110.

145
11:19.840 --> 11:25.960
So you can choose whichever way you find most intuitive, and if you want, you can even split this

146
11:25.960 --> 11:27.910
up into several steps, right?

147
11:27.940 --> 11:33.550
We could say that tip_as_percent = tip / 100.

148
11:34.300 --> 11:44.500
And then we can multiply the bill by the tip_as_percent to get the total_tip_amount.

149
11:46.420 --> 11:54.280
And then finally we can get the total_bill by adding the bill to the total_tip_amount.

150
11:54.640 --> 12:00.040
Feel free to do this whichever way makes sense to you, but I'm going to leave it with as many steps

151
12:00.040 --> 12:05.260
as possible so that you can actually work through the logic if you got stuck on this maths.

152
12:06.160 --> 12:11.850
Now, the next thing we need to do is to divide the total_bill by the number of people.

153
12:11.850 --> 12:15.540
So if we had five people, then we would divide it five ways, right?

154
12:15.570 --> 12:26.610
So let's call this bill_per_person, which is going to be equal to the total_bill divided by the number

155
12:26.610 --> 12:27.540
of people.

156
12:28.680 --> 12:33.930
Now at this stage, this bill_per_person is a floating point number,

157
12:33.930 --> 12:37.860
so it could have many many digits after the decimal point.

158
12:37.890 --> 12:44.670
If we want to round this to two decimal places, then you might remember from some of the lessons today,

159
12:44.670 --> 12:47.610
we have access to a function called round(),

160
12:47.640 --> 12:53.910
and here we can add the number that we want to round which is the bill_per_person,

161
12:53.910 --> 13:01.230
and then after a comma, we can specify how precise, how many numbers or how many decimal places do we

162
13:01.230 --> 13:02.850
want to round this bill to.

163
13:02.880 --> 13:06.030
And in our case it's two decimal places.

164
13:06.060 --> 13:15.120
So now this is the final amount which I'll call final_amount, which is the bill_per_person rounded

165
13:15.120 --> 13:17.070
to two decimal places.

166
13:17.100 --> 13:25.110
And we can now finally print this to the user and say, "Each person should pay..."

167
13:25.140 --> 13:31.350
And remember, you can either use string concatenation, which will require you to convert this number

168
13:31.350 --> 13:36.810
into a string again, or use the trick that we learned in the previous lessons, where we can create

169
13:36.810 --> 13:44.550
an f-string by adding f in front of the string, and then using some curly braces to insert the {final_amount}

170
13:44.550 --> 13:45.870
right here.

171
13:45.900 --> 13:49.140
And we can write the "{final_amount} dollars..."

172
13:49.140 --> 13:59.250
Or we can put a dollar sign in front and maybe a colon here and now if we clear our console and run

173
13:59.250 --> 14:01.710
our code then you'll see it work.

174
14:01.710 --> 14:03.360
So, "Welcome to the tip calculator!

175
14:03.390 --> 14:04.650
What was the total bill?"

176
14:04.680 --> 14:06.900
Let's make up some random number.

177
14:06.960 --> 14:08.520
"What percentage tip?"

178
14:08.520 --> 14:15.380
Let's say we're going to give 15 and then we're splitting it between five people and it calculates everything,

179
14:15.410 --> 14:17.120
doing all of that maths.

180
14:17.120 --> 14:23.810
And finally, it rounds it to two decimal places, giving us the final amount each person should pay.

181
14:24.500 --> 14:26.780
How did you get on with this project?

182
14:26.780 --> 14:30.590
Did you manage to get it in one go without having to look at the solution?

183
14:30.980 --> 14:36.380
If you got stuck at any point or if your code didn't work as you expect it to, this is the time to

184
14:36.380 --> 14:41.600
go back to it, fix it, and make sure that you've understood everything that's been covered through

185
14:41.600 --> 14:42.770
this exercise.

186
14:43.400 --> 14:48.260
So I hope you had fun building this project and doing all of the coding exercises and learning about

187
14:48.260 --> 14:54.710
mathematical operations, f-Strings, Data Types, Type Conversion, and all of the things that we did

188
14:54.740 --> 14:55.610
today.

189
14:56.000 --> 14:59.090
Have a rest and I'll be back tomorrow

190
14:59.090 --> 15:05.210
teaching you about Conditional Statements, and how we can get the program to do different things depending

191
15:05.210 --> 15:09.560
on certain conditions, or certain parts of our code.

192
15:09.590 --> 15:13.340
So I bid you good night and I'll see you tomorrow.