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