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