Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
Hey, guys, welcome to the final project of today, and in this final project, we're going to be building
2
a tip calculator.
3
So the final output is going to look like this.
4
It's going to print
5
Welcome to the tip calculator.
6
And it's going to ask you for an input for how much the total bill came to.
7
Let's say that we and a couple of friends went out for lunch and it came to 124.
8
56 dollars.
9
And then it'll ask you, what percentage tip would you like to give?
10
So let's say we want to give a 12 percent tip.
11
So we enter 12 and hit enter.
12
And then finally it asks how many people are splitting this bill?
13
Let's say there's seven of us and then hit enter.
14
Finally, it's going to tell us that each person should pay about $19.93.
15
Notice how the final bill, even though after all of these calculations, it's probably got more numbers
16
after the decimal point, we only want two decimal places of accuracy, so it should be rounded to two
17
decimal places.
18
The second thing to remember is that these are percentages, and in order to calculate a percentage of
19
something, you can multiply a number by the percentage number divided by 100.
20
Let me show you this in a little bit more detail.
21
If you head over to replit/appbrewery/tip-calculator-start, then you've got
22
these starting file for this project.
23
And here I just want to quickly show you how the math works, because this project is not about testing
24
your math.
25
It's about seeing how well you've understood the programming concepts.
26
Let's say that we had a bill of $150.
27
If we were to apply a 12% tip on top of that, then 12% is going to be equal to twelve
28
divided by a hundred, which is equal to 0.12.
29
Now the next step is we can multiply 150, the total bill, by 0.12.
30
And this will give us what 12 percent of 150 is, which is 18.
31
Now we of course, have to add the tip onto the final bill so it becomes 150 plus 18.
32
So $150 with a 12 percent tip is equal to $168.
33
Now, a shorthand way of doing all of this is simply multiplying 150 by 1.12.
34
So the 1 is 150, and then it's 0.12 on top of that.
35
So when we do this, it gives us pretty much the same number.
36
Now, if during your testing and your coding you come across some sort of number that looks a little
37
bit strange because you think 150 multiplied by 1.12 should actually equal 168
38
precisely.
39
So what are all of these extra numbers at the end?
40
Now, the short answer is you don't have to worry about it.
41
It's simply related to how Python processes these floating-point numbers.
42
If you're really interested and you want to read about this, then I'll link to this page in the Python
43
documentation where they tell you why this is happening.
44
But be warned, it's pretty dense and it's pretty heavy.
45
But the final conclusion is it's just the way that Python is approximating this number.
46
So you don't actually have to worry about this.
47
Now, once we've gotten to 168, the next step is to split it between five people.
48
So 168 divided by 5 is equal to 33.6.
49
Now, what we want to show the user is we want to show them the amount that they have to pay with two
50
decimal places of accuracy.
51
We want to say something like 33.60 if this is the case, because that's normally
52
how we represent numbers when it comes to money.
53
So what I want you to do is to also be able to round any of these numbers to two decimal places.
54
If you have successfully created this program, then it should work exactly as this example version
55
would, which you can find if you go to tip-calculator-end.appbrewery.repl.run, and
56
you'll be to see the formatting where it actually tells you each person should pay this particular dollar
57
amount and it's rounded to two decimal places.
58
And this is the amount after taking into consideration these three different inputs from the user.
59
Have a play around with the final product and then have a think about how you would create this program
60
step by step and then give it a go by forking this starting repository to your own account and then
61
try to achieve this result.
62
This is going to test everything that you've done so far including fstrings, including complex
63
mathematical operations,
64
including PEMDAS and calculating numbers and the order in which mathematical operations are run by
65
a computer, as well as everything you learn in the previous lesson and the previous days as well. I think
66
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
you need to split the bill between friends.
68
So I'm going to let you pause the video and get on with it.
69
And then once you're done, if you want to, head back over here and I'll walk through the solution with
70
you. But I really recommend you giving it a good go before you come back.
71
So pause the video now and I'll see you later.
72
OK, so we've seen how the program is supposed to work and we're trying to create it from scratch by
73
reverse-engineering it essentially. The first thing I'm going to do is I'm going to fork the starting
74
project and create my own version of it which I can edit.
75
Now, the first thing our program should say is welcome to the tip calculator.
76
So that's easy enough.
77
We just have to create a print statement.
78
Perfect.
79
And if you're creating a really enthusiastic tip calculator, you can replace the full stop with an
80
exclamation mark.
81
OK, so let's run it step by step to make sure that we don't have any errors along the way.
82
So the first step seems to be working. Now
83
the next step is to ask the user for some input.
84
And we want to know what was their total bill, what did it come to?
85
So let's go ahead and create an input and ask them what was the total bill?
86
And then we'll add a dollar sign or whatever currency it is that you prefer to work with.
87
Because when we actually run this, you'll see that the input will go straight after the end of that
88
string,
89
so at the dollar sign.
90
So now we can enter a dollar amount like this, but it's not really saved anywhere.
91
This data just disappears because we haven't stored it inside a variable.
92
So let's go ahead and do that.
93
Let's call this variable bill and we'll set it to equal whatever the user typed in to this input.
94
Now, remember that this bill is going to have a data type of a string, right?
95
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
of the bill is, as we said, a string. In order to be able to do math,
97
you might remember, we have to change this into a number format,
98
so a float or an int.
99
Now, in this case, because the bill is likely to have numbers after the decimal place, it's probably
100
better that we turn it into a float so that we get the most accurate result possible. Now that we're
101
done with the bill,
102
the next question the tip calculator should ask is what percentage tip would you like to give? 10, 12
103
or 15?
104
Or you can, of course, switch this up depending on how much tip you normally give.
105
So the next line is going to be another input.
106
Here, I'm asking the user, how much tip would you like to give and giving them some examples. And
107
notice how in my examples, I don't actually have the percentage sign here because I don't really
108
want them to type the percent sign into this input because if they do when they say 12%,
109
this is going to be really hard for me to turn the 12% into an actual number.
110
So you could, in your input message, say something like how much tip would you like to give 10,
111
12, 15?
112
Please don't add any percentage signs.
113
Just add the number or something like that.
114
But again, once we've done this, it's not stored anywhere unless we add it into a variable.
115
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
a whole number, 10, 12 or 15,
117
right?
118
So depending on what you think is best, you can either turn this into a float or in my case, I'm probably
119
going to convert it into an integer.
120
So now we're onto the final step, which is how many people to split the bill between.
121
So let's go ahead and create a variable called people.
122
And I'm going to add an int wrapper around this input so that even in the very beginning, we know that
123
we have to convert the number of people into a whole number because I've never had a meal with
124
3.5 before.
125
So the input is going to be how many people to split the bill.
126
And at this point, they should enter the number of people which will turn into an integer stored inside
127
this variable called people. So now that we've got all the data collected from the user, we're finally
128
ready to do some math.
129
And we said that the way to work out the tip is by multiplying it by 1. and then the number after
130
the decimal point is whatever percentage they decided to give.
131
So it would be 1.1 if it was 10 percent and 1.15 if it was 15 percent.
132
Let's go ahead and calculate the bill_with_tip.
133
So remember how we said that before the tip percentage is equal to the whole number, 10, 12 or 15,
134
divided by 100, which turns it into 0.1, 0.12 or 0.15,
135
and then once this calculation is done, we can multiply it by the bill. And then finally we add that
136
to the original bill.
137
So let's go ahead and print this and just check to see that it looks sensible.
138
So bill_with_tip is what we're going to print.
139
And let's put some easy numbers in there.
140
And let's say that the bill was a hundred dollars and the tip was 10 percent.
141
So then the 10% of hundred dollars is ten dollars.
142
So the bill_with_tip,
143
that's what's going to be printed next, should be 110. So we can type anything in here because
144
we're not using it.
145
And you can see that this calculation, bill_with_tip, is 110, which is what we want.
146
Now, there's a lot of other ways that you can express this.
147
For example, you could say, bill * (1 + tip/100).
148
So this would actually give you the same result as well.
149
But remember what we said previously about how Python is a little bit weird with floating-point numbers.
150
But essentially this is still giving us the same result, which is 110.
151
So you can choose whichever way you find most intuitive.
152
And if you want, you can even split this up into several steps, right?
153
We could say that tip_as_percent is equal to tip/100.
154
And then we can multiply the bill by the tip_as_percent to get the total tip amount.
155
And then finally, we can get the total bill by adding the bill to the total_tip_amount.
156
Feel free to do this whichever way makes sense to you, but I'm going to leave it with as many steps
157
as possible so that you can actually work through the logic if you got stuck on this math.
158
Now, the next thing we need to do is to divide the total bill by the number of people.
159
So if we had five people, then we would divide it five ways,
160
right?
161
So let's call this bill_per_
162
person, which is going to be equal to the total bill divided by the number of people.
163
Now, at this stage, this bill_per_person is a floating-point number, so it could have many, many
164
digits after the decimal point.
165
If we want to round this to two decimal places, then you might remember from some of the lessons today,
166
we have access to a function called round.
167
And here we can add the number that we want around, which is the bill_per_person,
168
and then after a comma, we can specify how precise, how many numbers or how many decimal places do
169
we want to round this bill to.
170
And in our case, it's two decimal places.
171
So now this is the final amounts, which I'll call final_amount which is the bill_per_person rounded
172
to two decimal places.
173
And we can now finally print this to the user and say each person should pay...
174
And remember, you can either use string concatenation which will require you to convert this number
175
into a string again, or use the trick that we learned in the previous lessons where we can create an
176
fstring by adding F in front of the string and then using some curly braces to insert the final amount
177
right here.
178
And we can write the final_amount dollars or we can put a dollar sign in front and maybe a colon here.
179
And now if we clear our console and run our code, then you'll see it work.
180
So welcome to the tip calculator.
181
What was the total bill?
182
Let's make up some random number.
183
What percentage tip?
184
Let's say we're going to get 15 and then we're splitting it between five people. And it calculates everything,
185
doing all of that maths.
186
And finally, it rounds it to two decimal places, giving us the final amount each person should pay.
187
But what if we test it out with the initial numbers?
188
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
a 12 percent tip and we're going to split it between five people.
190
Notice that is telling us that each person should pay $33.6.
191
But normally, we're used to seeing that rounded to two decimal places.
192
But we have that round function here already.
193
So why is it not doing its job?
194
Well, if you think about it, the result of this calculation is just 33.6. There's
195
nothing after the six, but we want it to display a zero. And this is a formating problem rather than
196
a mathematical rounding problem.
197
So what do we do?
198
Well, we go to our good friend Google whenever we're stuck and we're going to search for how to round
199
number to two decimal places in Python.
200
And if we take a look at the first Stack Overflow question, you can see that this person had pretty much
201
the same problem that we had.
202
And if we're scrolling down, you can see that the solution is to use some sort of formatting. And we
203
can try out each of these examples one by one in code to see how they work,
204
and we can also take a look at the documentation that they link to.
205
So the actual way that we do this is by using a colon and then dot and then we specify that after the
206
dot, we want two decimal points in our float. And we use the format function to do this.
207
So instead of using round, we can create this final amount variable by creating a string using that
208
format, :.2f, and then we can use the format function to pass in that bill_per_person.
209
And now when we run this again, using the same inputs, we now get a .60 at the very end
210
and this is basically turned this bill_per_person, which is a float into a string.
211
And that string is abiding by this particular format, which is the two decimal places.
212
How did you get on with this project?
213
Did you manage to get it in one go without having to look at the solution? If you got stuck at any point
214
or if your code didn't work as you expect it to,
215
this is a time to go back to it, fix it, and make sure that you've understood everything that's been
216
covered through this exercise.
217
But if you already created it and it's working perfectly, then you're ready to go out there with your
218
tip calculator in hand.
219
Remember that you can save the URL of your tip calculator in your phone as a bookmark.
220
So the next time you go out and have dinner or lunch with friends, then you can crack open your own
221
tip calculator and figure out the final bill.
222
So I hope you had fun building this project and doing all of the coding exercises and learning about mathematical
223
operations, fstrings, data types, type conversion, and all of the things that we did today. Have a rest
224
and I'll be back tomorrow teaching you about conditional statements and how we can get the program to
225
do different things depending on certain conditions or certain parts of our code.
226
So I bid you good night and I'll see you tomorrow.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.