Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,070 --> 00:00:00,610
Hey guys,
1
2
00:00:00,610 --> 00:00:05,610
Welcome to the final project of today and in this final project we're going to
2
3
00:00:06,370 --> 00:00:08,680
be building a tip calculator.
3
4
00:00:09,310 --> 00:00:12,730
So the final output is going to look like this. It's going to print
4
5
00:00:12,790 --> 00:00:14,350
Welcome to the tip calculator,
5
6
00:00:14,680 --> 00:00:19,210
and it's going to ask you for an input for how much the total bill came to.
6
7
00:00:19,870 --> 00:00:24,870
Let's say that we and a couple of friends went out for lunch and it came to
7
8
00:00:25,810 --> 00:00:30,810
$124.56 and then it'll ask you what percentage tip would you like to give?
8
9
00:00:33,520 --> 00:00:38,500
So let's say we want to give a 12% tip, so we entered 12 and hit enter,
9
10
00:00:39,250 --> 00:00:42,550
and then finally it asks how many people are splitting this bill?
10
11
00:00:42,580 --> 00:00:46,510
Let's say there is a seven of us and then hit enter. Finally,
11
12
00:00:46,510 --> 00:00:51,510
it's going to tell us that each person should pay about $19.93. Notice how the
12
13
00:00:53,050 --> 00:00:56,320
final bill, even though after all of these calculations,
13
14
00:00:56,320 --> 00:00:59,590
it's probably got more numbers after the decimal point,
14
15
00:01:00,070 --> 00:01:03,850
we only want two decimal places of accuracy,
15
16
00:01:03,850 --> 00:01:06,640
so it should be rounded to two decimal places.
16
17
00:01:07,300 --> 00:01:12,300
The second thing to remember is that these are percentages and in order to
17
18
00:01:12,850 --> 00:01:14,920
calculate a percentage of something,
18
19
00:01:15,250 --> 00:01:20,250
you can multiply a number by the percentage number divided by a hundred. Let me
19
20
00:01:21,550 --> 00:01:23,890
show you this in a little bit more detail.
20
21
00:01:24,430 --> 00:01:28,930
If you head over to repl.it/@appbrewery/tip-calculator-
21
22
00:01:28,930 --> 00:01:29,763
start,
22
23
00:01:29,920 --> 00:01:34,920
then you've got these starting file for this project. And here I just want to
23
24
00:01:35,530 --> 00:01:39,670
quickly show you how the math works because this project is not about testing
24
25
00:01:39,670 --> 00:01:40,150
your maths.
25
26
00:01:40,150 --> 00:01:43,690
It's about seeing how well you've understood the programming concepts.
26
27
00:01:44,260 --> 00:01:49,260
Let's say that we had a bill of $150. If we were to apply a 12% tip on top of
27
28
00:01:53,710 --> 00:01:54,340
that,
28
29
00:01:54,340 --> 00:01:59,340
then 12% is going to be equal to 12 / 100 which is equal to
29
30
00:02:00,880 --> 00:02:05,880
0.12. Now the next step is we can multiply 150, the total bill, by 0.12 and this
30
31
00:02:10,090 --> 00:02:13,450
will give us what 12% of 150 is
31
32
00:02:13,480 --> 00:02:18,250
which is 18. Now we of course have to add the tip onto the final bill,
32
33
00:02:18,250 --> 00:02:23,250
so it becomes 150 + 18. So $150 with 12% tip is equal to $168. Now a
33
34
00:02:28,180 --> 00:02:33,180
shorthand way of doing all of this is simply multiplying 150 by 1.12. So the one
34
35
00:02:37,690 --> 00:02:42,520
is 150 and then it's 0.12 on top of that.
35
36
00:02:43,090 --> 00:02:46,600
So when we do this, it gives us pretty much the same number.
36
37
00:02:47,440 --> 00:02:50,020
Now if during your testing and your coding,
37
38
00:02:50,020 --> 00:02:54,130
you come across some sort of number that looks a little bit strange because you
38
39
00:02:54,130 --> 00:02:59,130
think 150 multiplied by 1.12 should actually equal 168 precisely.
39
40
00:03:00,160 --> 00:03:02,950
So what are all of these extra numbers at the end?
40
41
00:03:03,460 --> 00:03:06,400
Now the short answer is you don't have to worry about it.
41
42
00:03:06,520 --> 00:03:11,520
It's simply related to how Python processes these floating point numbers.
42
43
00:03:12,310 --> 00:03:15,130
If you're really interested and you want to read about this,
43
44
00:03:15,400 --> 00:03:20,400
then I'll link to this page in the Python documentation where they tell you why
44
45
00:03:20,680 --> 00:03:24,730
this is happening. But be warned, it's pretty dense and it's pretty heavy.
45
46
00:03:25,060 --> 00:03:29,650
But the final conclusion is it's just a way that Python is approximating this
46
47
00:03:29,650 --> 00:03:32,590
number. So you don't actually have to worry about this.
47
48
00:03:33,160 --> 00:03:38,160
Now once we've gotten to 168, the next step is the split it between five people.
48
49
00:03:38,590 --> 00:03:43,590
So 168 divided by five is equal to 33.6. Now what we want to show the user is we
49
50
00:03:48,100 --> 00:03:52,330
want to show them the amount that they have to pay with two decimal places of
50
51
00:03:52,330 --> 00:03:52,870
accuracy.
51
52
00:03:52,870 --> 00:03:57,280
So we want to say something like 33.60 if this is the case because that's
52
53
00:03:57,280 --> 00:04:00,820
normally how we represent numbers when it comes to money.
53
54
00:04:01,360 --> 00:04:06,360
So what I want you to do is to also be able to round any of these numbers to two
54
55
00:04:06,640 --> 00:04:10,840
decimal places. If you have successfully created this program,
55
56
00:04:11,140 --> 00:04:15,550
then it should work exactly as this example version would,
56
57
00:04:15,850 --> 00:04:18,580
which you can find if you go to tip-calculator
57
58
00:04:18,580 --> 00:04:23,580
-end.appbrewery.repl.run and you'll be to see the formatting where it
58
59
00:04:24,280 --> 00:04:29,170
actually tells you each person should pay this particular dollar amount and it's
59
60
00:04:29,170 --> 00:04:31,480
rounded to two decimal places.
60
61
00:04:31,870 --> 00:04:36,870
And this is the amount after taking into consideration these three different
61
62
00:04:37,150 --> 00:04:38,410
inputs from the user.
62
63
00:04:39,040 --> 00:04:42,910
Have a play around with the final product and then have a think about how you
63
64
00:04:42,910 --> 00:04:47,620
would create this program step-by-step and then give it a go by forking this
64
65
00:04:47,650 --> 00:04:52,150
starting repository to your own account and then try to achieve this result.
65
66
00:04:52,990 --> 00:04:56,950
This is going to test everything that you've done so far, including f strings,
66
67
00:04:56,980 --> 00:04:59,920
including complex mathematical operations,
67
68
00:05:00,040 --> 00:05:03,460
including PEMDAS and calculating numbers
68
69
00:05:03,520 --> 00:05:08,520
and the order in which mathematical operations are run by a computer as well as
69
70
00:05:09,310 --> 00:05:12,550
everything you've learned in the previous lesson in previous days as well.
70
71
00:05:13,090 --> 00:05:16,330
I think this is going to be a lot of fun and the end result is going to be kind
71
72
00:05:16,330 --> 00:05:19,660
of useful the next time that you need to split the bill between friends.
72
73
00:05:20,080 --> 00:05:22,270
So I'm going to let you pause a video and get on with it.
73
74
00:05:22,660 --> 00:05:23,710
And then once you're done,
74
75
00:05:23,770 --> 00:05:27,490
if you want to, head back over here and I'll walk through the solution with you.
75
76
00:05:27,820 --> 00:05:31,810
But I really recommend you giving it a good go before you come back.
76
77
00:05:32,080 --> 00:05:34,300
So pause the video now and I'll see you later.
77
78
00:05:34,830 --> 00:05:39,150
[inaudible]
78
79
00:05:39,940 --> 00:05:40,270
Okay,
79
80
00:05:40,270 --> 00:05:43,840
so we've seen how the program is supposed to work and we're trying to create it
80
81
00:05:43,840 --> 00:05:47,050
from scratch by reverse engineering it essentially.
81
82
00:05:47,770 --> 00:05:52,770
The first thing I'm going to do is I'm going to fork the starting project and
82
83
00:05:53,200 --> 00:05:55,480
create my own version of it, which I can edit.
83
84
00:05:56,170 --> 00:05:59,900
Now the first thing our program should say is welcome to the tip calculator.
84
85
00:06:00,200 --> 00:06:04,940
So that's easy enough. We just have to create a print statement. Perfect.
85
86
00:06:05,120 --> 00:06:08,150
And if you're creating a really enthusiastic tip calculator,
86
87
00:06:08,180 --> 00:06:12,410
you can replace the full stop with an exclamation Mark. Okay,
87
88
00:06:12,440 --> 00:06:16,250
so let's run it step by step to make sure that we don't have any errors along
88
89
00:06:16,250 --> 00:06:19,550
the way. So the first step seems to be working.
89
90
00:06:19,910 --> 00:06:24,440
Now the next step is to ask the user for some inputs and we want to know what
90
91
00:06:24,440 --> 00:06:26,660
was their total bill, what did it come to?
91
92
00:06:27,230 --> 00:06:32,230
So let's go ahead and create an input and ask them what was the total bill,
92
93
00:06:32,990 --> 00:06:37,580
and then we'll add a dollar sign or whatever currency it is that you prefer to
93
94
00:06:37,580 --> 00:06:40,310
work with because when we actually run this,
94
95
00:06:40,610 --> 00:06:45,410
you'll see that the input will go straight after the end of that string,
95
96
00:06:45,440 --> 00:06:50,030
so at the dollar sign. So now we can enter a dollar amount like this,
96
97
00:06:50,360 --> 00:06:52,190
but it's not really saved anywhere.
97
98
00:06:52,220 --> 00:06:56,930
This data just disappears because we haven't stored it inside a variable.
98
99
00:06:57,350 --> 00:06:58,730
So let's go ahead and do that.
99
100
00:06:58,760 --> 00:07:03,760
Let's call this variable bill and we'll set it to equal whatever the user typed
100
101
00:07:04,460 --> 00:07:05,660
into this input.
101
102
00:07:06,290 --> 00:07:11,290
Now remember that this bill is going to have a data type of a string,
102
103
00:07:13,880 --> 00:07:14,300
right?
103
104
00:07:14,300 --> 00:07:19,070
So if I go ahead and hit run and let's just type some numbers in here,
104
105
00:07:19,490 --> 00:07:22,790
you'll see that the data type of the bill is, as we said,
105
106
00:07:22,970 --> 00:07:25,490
a string. In order to be able to do maths,
106
107
00:07:25,910 --> 00:07:30,710
you might remember we have to change this into a number format,
107
108
00:07:30,710 --> 00:07:33,680
so a float or an int. Now in this case,
108
109
00:07:33,710 --> 00:07:37,130
because the bill is likely to have numbers after the decimal place,
109
110
00:07:37,460 --> 00:07:42,230
it's probably better that we turn it into a float so that we get the most
110
111
00:07:42,380 --> 00:07:46,910
accurate result possible. Now that we're done with the bill,
111
112
00:07:47,030 --> 00:07:52,030
the next question the tip calculator should ask is what percentage tip would
112
113
00:07:52,370 --> 00:07:57,370
you like to give 10, 12, or 15, or you can, of course, switch this up depending on
113
114
00:07:57,590 --> 00:07:59,030
how much tip you normally give.
114
115
00:07:59,660 --> 00:08:02,780
So the next line is going to be another input.
115
116
00:08:04,850 --> 00:08:05,210
Here,
116
117
00:08:05,210 --> 00:08:08,240
I'm asking the user how much tip would you like to give and giving them some
117
118
00:08:08,240 --> 00:08:11,120
examples and notice how in my examples,
118
119
00:08:11,420 --> 00:08:16,420
I don't actually have the percentage sign here because I don't really want them
119
120
00:08:16,640 --> 00:08:20,570
to type the percent sign into this input.
120
121
00:08:20,960 --> 00:08:22,730
Because if they do,
121
122
00:08:22,820 --> 00:08:27,820
when they say 12%, this is going to be really hard for me to turn the 12% into an
122
123
00:08:31,040 --> 00:08:35,510
actual number. So you could, in your input message,
123
124
00:08:35,540 --> 00:08:38,270
say something like, how much tip would you like to give?
124
125
00:08:38,660 --> 00:08:42,170
10, 12, 15? Please don't add any percentage signs,
125
126
00:08:42,320 --> 00:08:47,000
just add the number or something like that. But again, once we've done this,
126
127
00:08:47,030 --> 00:08:51,590
it's not stored anywhere unless we add it into a variable.
127
128
00:08:52,040 --> 00:08:57,040
Now we've got a bill that stores the total bill as a float and we've got a tip,
128
129
00:08:58,560 --> 00:09:02,460
which is going to be a whole number 10, 12, or 15 right?
129
130
00:09:02,850 --> 00:09:04,680
So depending on what you think is best,
130
131
00:09:04,680 --> 00:09:09,420
you can either turn this into a float or in my case I'm probably going to
131
132
00:09:09,420 --> 00:09:11,730
convert it into an integer.
132
133
00:09:12,510 --> 00:09:14,550
So now we're onto the final step,
133
134
00:09:14,610 --> 00:09:17,220
which is how many people to split the bill between.
134
135
00:09:17,700 --> 00:09:21,060
So let's go ahead and create a variable called people
135
136
00:09:21,540 --> 00:09:26,540
and I'm going to add an int wrapper around this input so that even in the very
136
137
00:09:28,470 --> 00:09:28,980
beginning,
137
138
00:09:28,980 --> 00:09:33,750
we know that we have to convert the number of people into a whole number because
138
139
00:09:33,780 --> 00:09:36,870
I've never had a meal with 3.5 people before.
139
140
00:09:37,800 --> 00:09:41,850
So the input is going to be how many people to split the bill.
140
141
00:09:43,440 --> 00:09:47,820
And at this point they should enter the number of people which will turn into an
141
142
00:09:47,850 --> 00:09:51,630
integer stored inside this variable called people.
142
143
00:09:52,140 --> 00:09:55,470
So now that we've got all the data collected from the user,
143
144
00:09:55,590 --> 00:09:57,480
we're finally ready to do some maths.
144
145
00:09:58,050 --> 00:10:03,050
And we said that the way to work out the tip is by multiplying it by 1.
145
146
00:10:06,840 --> 00:10:11,670
and then the number after the decimal point is whatever percentage they decided
146
147
00:10:11,670 --> 00:10:12,503
to give.
147
148
00:10:12,540 --> 00:10:17,540
So it would be 1.1 if it was 10% and 1.15 if it was 15%. Let's go ahead and
148
149
00:10:19,860 --> 00:10:23,580
calculate the bill_with_tip.
149
150
00:10:24,960 --> 00:10:29,960
So remember how we said that before the tip percentage is equal to the whole
150
151
00:10:30,750 --> 00:10:31,200
number,
151
152
00:10:31,200 --> 00:10:36,200
10, 12, or 15, divided by a hundred which turns it into not 0.1 or 0.12 or 0.15.
152
153
00:10:38,730 --> 00:10:41,160
And then once this calculation is done,
153
154
00:10:41,370 --> 00:10:46,370
we can multiply it by the bill. And then finally we add that to the original
154
155
00:10:47,460 --> 00:10:48,293
bill.
155
156
00:10:49,050 --> 00:10:54,050
So let's go ahead and print this and just check to see that it looks sensible.
156
157
00:10:55,050 --> 00:11:00,050
So bill_with_tip is what we're going to print and let's put some easy numbers in
157
158
00:11:00,810 --> 00:11:01,643
there.
158
159
00:11:02,010 --> 00:11:07,010
Let's say that the bill was $100 and the tip was 10% so then the
159
160
00:11:08,670 --> 00:11:12,570
10% of a hundred dollars $10 so the bill_with_tip,
160
161
00:11:12,780 --> 00:11:17,780
that's what's going to be printed next should be 110. So we can type anything in
161
162
00:11:18,000 --> 00:11:19,320
here because we're not using it
162
163
00:11:19,680 --> 00:11:24,240
and you can see that this calculation bill_with_tip is 110 which is what we
163
164
00:11:24,240 --> 00:11:28,320
want. Now, there's a lot of other ways that you can express this.
164
165
00:11:28,800 --> 00:11:29,400
For example,
165
166
00:11:29,400 --> 00:11:34,400
you could say bill * (1 + tip /100). So this would
166
167
00:11:38,010 --> 00:11:40,620
actually give you the same result as well.
167
168
00:11:41,220 --> 00:11:45,150
But remember what we said previously about how Python is a little bit weird with
168
169
00:11:45,660 --> 00:11:50,340
floating point numbers, but essentially this is still giving us the same result,
169
170
00:11:50,370 --> 00:11:55,370
which is 110. So you can whichever way you find most intuitive
170
171
00:11:56,020 --> 00:12:00,280
and if you want, you can even split this up into several steps, right?
171
172
00:12:00,340 --> 00:12:05,340
We could say that tip_as_percent is equal to tip / 100
172
173
00:12:07,120 --> 00:12:12,120
and then we can multiply the bill by the tip_as_percent to get the total_tip
173
174
00:12:15,820 --> 00:12:16,653
_amount.
174
175
00:12:19,030 --> 00:12:24,030
And then finally we can get the total_bill by adding the bill to the total_tip
175
176
00:12:25,690 --> 00:12:29,920
_amount. Feel free to do this whichever way make sense to you,
176
177
00:12:30,280 --> 00:12:33,910
but I'm going to leave it with as many steps as possible so that you can
177
178
00:12:33,910 --> 00:12:37,360
actually work through the logic if you got stuck on this math.
178
179
00:12:38,740 --> 00:12:43,510
Now the next thing we need to do is to divide the total bill by the number of
179
180
00:12:43,510 --> 00:12:47,680
people. So if we had five people, then we would divide it five ways, right?
180
181
00:12:48,160 --> 00:12:49,840
So let's call this um,
181
182
00:12:49,870 --> 00:12:53,560
bill_per_person,
182
183
00:12:54,130 --> 00:12:59,130
which is going to be equal to the total_bill divided by the number of people.
183
184
00:13:01,270 --> 00:13:02,560
Now at this stage,
184
185
00:13:02,650 --> 00:13:06,280
this bill_per_person is a floating point number,
185
186
00:13:06,340 --> 00:13:09,970
so it could have many, many digits after the decimal point.
186
187
00:13:10,480 --> 00:13:13,630
If we want to round this to two decimal places,
187
188
00:13:14,020 --> 00:13:18,400
then you might remember from some of the lessons today we have access to a
188
189
00:13:18,400 --> 00:13:19,750
function called round.
189
190
00:13:20,320 --> 00:13:25,320
And here we can add the number that we want to round which is the bill_per_person
190
191
00:13:26,590 --> 00:13:30,340
and then after a comma, we can specify how precise,
191
192
00:13:30,340 --> 00:13:34,960
how many numbers or how many decimal places do we want to round this bill to.
192
193
00:13:35,500 --> 00:13:38,050
And in our case, it's two decimal places.
193
194
00:13:38,740 --> 00:13:41,950
So now this is the final amount
194
195
00:13:42,130 --> 00:13:44,500
which I'll call final_amount,
195
196
00:13:44,980 --> 00:13:49,150
which is the bill per person rounded to two decimal places.
196
197
00:13:49,780 --> 00:13:54,780
And we can now finally print this to the user and say each person should pay.
197
198
00:13:57,850 --> 00:14:01,210
And remember, you can either use string concatenation,
198
199
00:14:01,450 --> 00:14:04,570
which will require you to convert this number into a string
199
200
00:14:04,570 --> 00:14:08,800
again or use the trick that we learned in the previous lessons where we can
200
201
00:14:08,800 --> 00:14:13,800
create an F string by adding F in front of the string and then using some curly
201
202
00:14:14,290 --> 00:14:19,290
braces to insert the final amount right here. And we can write the final_amount
202
203
00:14:20,830 --> 00:14:25,830
dollars or we can put a dollar sign in front and maybe a colon here and now if
203
204
00:14:28,990 --> 00:14:32,260
we clear our console and run our code,
204
205
00:14:32,350 --> 00:14:35,710
then you'll see it work. So welcome to the tip calculator.
205
206
00:14:35,710 --> 00:14:38,950
What was the total bill? Let's make up some random number.
206
207
00:14:39,550 --> 00:14:40,840
What percentage tip?
207
208
00:14:40,870 --> 00:14:45,430
Let's say we're going to get 15 and then we're splitting it between five people.
208
209
00:14:46,390 --> 00:14:49,330
And it calculates everything doing all of that maths.
209
210
00:14:49,720 --> 00:14:52,850
And finally it rounds it to two decimal places,
210
211
00:14:53,120 --> 00:14:55,910
giving us the final amount each person should pay.
211
212
00:14:56,960 --> 00:14:59,060
How did it get on with this project?
212
213
00:14:59,240 --> 00:15:02,660
Did you manage to get it in one go without having to look at the solution?
213
214
00:15:03,590 --> 00:15:07,610
If you got stuck at any point or if your code didn't work as you expect it to,
214
215
00:15:07,970 --> 00:15:10,040
this is a time to go back to it, fix it,
215
216
00:15:10,370 --> 00:15:14,120
and make sure that you've understood everything that's been covered through this
216
217
00:15:14,120 --> 00:15:18,050
exercise. But if you've already created it and it's working perfectly,
217
218
00:15:18,440 --> 00:15:21,260
then you're ready to go out there with your tip calculator in hand.
218
219
00:15:21,680 --> 00:15:26,420
Remember that you can save the URL of your tip calculator in your phone as a
219
220
00:15:26,420 --> 00:15:29,690
bookmark. So the next time you go out and have dinner or lunch with friends,
220
221
00:15:29,990 --> 00:15:34,460
then you can crack open your own tip calculator and figure out the final bill.
221
222
00:15:35,330 --> 00:15:39,320
So hope you had fun building this project and doing all of the coding exercises
222
223
00:15:39,380 --> 00:15:43,760
and learning about mathematical operations, F strings, data types,
223
224
00:15:43,820 --> 00:15:44,990
type conversion,
224
225
00:15:45,050 --> 00:15:48,980
and all of the things that we did today. Have a rest
225
226
00:15:49,100 --> 00:15:54,100
and I'll be back tomorrow teaching you about conditional statements and how we
226
227
00:15:54,110 --> 00:15:59,110
can get the program to do different things depending on certain conditions or
227
228
00:15:59,600 --> 00:16:01,160
certain parts of our code.
228
229
00:16:01,760 --> 00:16:05,030
So I bid you good night and I'll see you tomorrow.
23181
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.